当应用在后台时如何进行活动



当我的应用在前景中时,它会相应地运行以下代码:

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
        }
    }

有一个通知并且我的应用在后台时,它不会运行上面的第一个摘要代码,然后转到指定的活动。它只是打开了打开MainAttivity的应用程序。

即使有通知时我的应用在后台,我该如何将其转入指定的活动?

 // 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"创建活动的新实例。

update

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

相关内容

  • 没有找到相关文章

最新更新