【问题标题】:Firebase Cloud Messaging : push notification and navigate to specific page of applicationFirebase Cloud Messaging:推送通知并导航到应用程序的特定页面
【发布时间】:2023-04-01 09:47:01
【问题描述】:

我正在为我的 android 应用程序使用 Firebase Cloud Messaging。我上次使用 GCMBaseIntentService 进行推送通知。我发现 Firebase Cloud Messaging,FirebaseMessagingService 与 GCMBaseIntentService() 不同。对于 GCMBaseIntentService,onMessage() 函数总是在应用程序收到推送通知时被调用。当我点击推送通知时,我可以操作接收到的数据并导航到特定页面。

但是,我意识到 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());
}

【问题讨论】:

    标签: android firebase notifications firebase-cloud-messaging


    【解决方案1】:

    使用数据消息代替通知。数据消息总是加载 onMessageReceived 类。

    【讨论】:

    • 主要问题是 onMessageReceived() 函数根本没有被调用。我在 android studio 中的 logcat 没有在 onMessageReceived() 函数中显示我的日志消息。或者其他任何其他解决方案让我获得 PushNotication 的特定内容,以便我可以导航到 Application 的特定页面
    • 我认为您正在发送通知消息
    • 这意味着这与前端 android 无关。我必须修改我的后端服务器端脚本?
    • 是的,你必须
    猜你喜欢
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2022-11-30
    • 2019-04-29
    • 2020-07-31
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多