通过通知

时间:2017-12-13 11:22:58

标签: android notifications activity-stack

我在我的应用中使用FCM (Firebase Cloud Messaging)

我的应用中有N个活动

在应用程序的生命周期中,它可能有任何活动堆栈:

Activity1 - > Activity2 - > Activity3 - > ... - > ActivityN

我想实现行为:

  1. 转到ActivityN
  2. 将应用程序转为后台
  3. 点击通知
  4. 在ActivityN上将应用程序转到前台
  5. 在ActivityN上显示对话框
  6. 如何实现呢?

3 个答案:

答案 0 :(得分:0)

FirebaseMessagingService类>> onMessageRecived

Intent intent = new Intent(this, ActivityN.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                FLAG_ONE_SHOT);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable._logo)
            .setContentText(notification)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

答案 1 :(得分:0)

如果您不确定要在通知中打开哪个活动,请单击。然后你应该在Notification点击广播。 创建一个广播重用器。

   class NotificationClickReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // here you will get Intent
        // Check which activity is currently open and pass the data to it
        // For passing data you can use a Another BroadcastReceiver or
    }
}

清单中的条目

<receiver
        android:name=".NotificationClickReceiver"
        />

然后使用pendingIntent通知getBroadcast点击

Intent intent = new Intent(this, NotificationClickReceiver.class);
    intent.putExtra("key","val");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 3, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable._logo)
            .setContentText(notification)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

现在您将了解 Notification ClickReceiver onReceive ,然后您可以确定将其传递给哪个活动。

答案 2 :(得分:0)

I had the same issue in that I wanted the app to just resume in its current state when the Firebase background notification arrived. Part of my solution from this answer.

In my case, I had up to four activities on the stack: A->B->C->D.

  1. In my app's manifest, I made activity D a singleTask activity (launchMode="singleTask") and put the intent-filter in that activity.

    <activity android:name=".activityD"> <intent-filter> <action android:name=".activityD" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

  2. I then put activity D as the "click_action" in the notification sent by Firebase Messaging.

  3. In my case, I didn't override the onNewIntent() method in activity D since the FirebaseMessageService's onMessageReceived method will get called when the app is brought to the foreground.
  4. Most important: Because activity D is a singleTask activity, it is only created if it's not already on the stack. Therefore, in activity D's onCreate() method, I check the contents of the Intent extras. If the extras didn't come from activity C, I pop D immediately (finish();return;) because I know it was created by the Firebase notification. That resumes the app's activity (A, B or C) when it went into the background. If the app's activity was D when it went into the background, onNewIntent() method will be called instead of onCreate() in which case I do nothing as described in step #3.

After thinking about it some more, a more general solution would be to create a "dummy" singleTask activity with the intent filter, then always pop it off the stack in the onCreate() method to resume the current activity when it was put in the background. Surely there's a better way... I just couldn't find it.