代码之家  ›  专栏  ›  技术社区  ›  Chan Yoong Hon

Firebase云消息传递:推送通知并导航到应用程序的特定页面

  •  1
  • Chan Yoong Hon  · 技术社区  · 9 年前

    然而,我意识到FirebaseMessagingService中的onMessageReceived()没有被调用。请问,当我收到推送通知时,如何导航到应用程序的特定位置。问题是,我需要根据收到的推送通知的内容导航到应用程序中的不同页面。

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage.getNotification().getBody());
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
    
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, HomeControllerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    
    1 回复  |  直到 9 年前
        1
  •  3
  •   Anil    9 年前

    使用 而不是 。数据消息始终在MessageReceived上加载类。

    推荐文章