2017-02-28 115 views
0

當我的應用程序是在前臺,其相應的運行下面的代碼:如何在應用程序處於後臺時進入活動?

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的應用程序。

即使我的應用在後臺出現通知時,我如何才能使其進入活動狀態?

+0

你是否努力使活動*彈出*即使你的應用程序在後臺? –

+0

是的,直接進入我指定的@AL活動。 – kylas

+0

通常,在沒有任何用戶交互的情況下彈出應用程序是*糟糕的UX *。想想[彈出式廣告](https://en.wikipedia.org/wiki/Pop-up_ad)。 –

回答

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」 這創造活動的新實例。

UPDATE

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

這個很好用。另一個問題是通知僅在我的應用程序位於MainActivity和後臺時顯示。當應用程序處於其他活動狀態時沒有任何通知。只有通知聲音。 – kylas

相關問題