【发布时间】: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