簡體   English   中英

Android-我的活動如何檢測到我在Firebase Messaging Service中收到了通知?

[英]Android - How can my Activity detect that I received a notification in Firebase Messaging Service?

讓我與您討論一個場景,讓該應用程序處於活動“ X”打開的前台。 現在,我的應用程序通過Firebase Messaging Service收到了通知。 我想要的是? 我希望如果我的應用程序位於前台,我的活動“ X”可以檢測到是否收到通知,以便可以切換圖標。

這是我的Firebase Messaging Service代碼:

 @Override
public void onMessageReceived(RemoteMessage message){
    type=message.getData().get("type");
    CrucialData.setReceivedNotificationWithoutChecking(true);
    createNotification(message);
}

private void createNotification(RemoteMessage message) {
    Context context = getBaseContext();
    checkingIfAppIsInForeground(context);
    if(!inForeground){
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.appicon)
                .setColor(getResources().getColor(R.color.colorPrimaryDark))
                .setContentTitle("Your chat is going to expire tomorrow!")
                .setAutoCancel(true);

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, main_activity.class);

        resultIntent.putExtra("launchedFromNotification",true);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your app to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(main_activity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mNotificationId is a unique integer your app uses to identify the
        mNotificationManager.notify(1231, mBuilder.build());
    }

您可以使用本地廣播在服務和活動之間進行通信。

這是一個例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM