应用程序在后台时如何进入活动?

时间:2017-02-28 10:23:40

标签: android firebase firebase-cloud-messaging

当我的应用程序位于前台时,它会相应地运行以下代码:

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(intent.getAction().equals(Config.REGISTRATION_COMPLETE)){
                    FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
                    displayFirebaseRegId();
                }else if(intent.getAction().equals(Config.PUSH_NOTIFICATION)){
                    String message = intent.getStringExtra("message");
                    NotificationCompat.Builder mBuilder =
                            new NotificationCompat.Builder(MainActivity.this)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle("ABC")
                                    .setContentText(message);
                    // Creates an explicit intent for an Activity in your app
                    Intent resultIntent = new Intent(context, SearchActivity.class);
                    resultIntent.putExtra("selectedTitle",message);
                    // This ensures that navigating backward from the Activity leads out of
                    // your application to the Home screen.
                    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                    stackBuilder.addParentStack(MainActivity.class);
                    stackBuilder.addNextIntent(resultIntent);
                    PendingIntent resultPendingIntent =
                            stackBuilder.getPendingIntent(
                                    0,
                                    PendingIntent.FLAG_UPDATE_CURRENT
                            );
                    mBuilder.setContentIntent(resultPendingIntent);
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify(123, mBuilder.build());

以下是处理通知的代码:

private void handleNotification(String message) {
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        }else{
            // If the app is in background, firebase itself handles the notification
        }
    }

当有通知且我的应用处于后台时,它不会运行上面的第一个代码段并转到指定的活动。它只是打开打开MainActivity的应用程序。

如果有通知,即使我的应用程序在后台,我如何才能按指定进入活动状态?

1 个答案:

答案 0 :(得分:1)

 // Creates an explicit intent for an Activity in your app
   Intent resultIntent = new Intent(context, SearchActivity.class);
   resultIntent.putExtra("selectedTitle",message);
**notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);**

你应该添加标志为" Intent.FLAG_ACTIVITY_CLEAR_TOP" 这会创建一个新的活动实例。

<强>更新

PendingIntent resultPendingIntent = 
                 stackBuilder.getPendingIntent(0,resultIntent);