[英]Firebase Cloud Messaging : push notification and navigate to specific page of application
我正在為我的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());
}
使用數據消息代替通知 。 數據消息始終加載onMessageReceived類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.