2017-07-12 46 views
0

發射活動後,我啓動從notification我收到activity。如果notification被按下,它將啓動一個activity。如果有對back-stack沒有以前activities,或者只有某一個,我想刪除某些activity,並在那裏,比新活動插入我的主要activity讓我們回到主要活動從通知

我發現這個Thread,但我不明白他怎麼有兩個意圖和標誌處理它。

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)

是否明智做到這一點,他做到了,或者我應該編輯該活動堆棧的方式嗎?

我對android開發相當新,所以有些建議可以幫助我。 非常感謝;)

更新:所以我去了stackbuilder,但不知何故它沒有設置正確...我沒有找到我的錯誤,我的布爾noActivity肯定設置,但我想某種方式我誤解了堆棧如何將之前的活動放在那裏。

private void sendNotification(messageType type, Map<String, String> data, boolean noActivities) { 
    Intent i; 
    String message = ""; 

    switch (type) { 
     case newFOLLOWER: 
      User cur = new User(data.get("other.name")); 
      User.lookAT = User.getOtherUserByName(cur); 
      i = new Intent(this, other_profile.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      message = data.get("other.name") + " is following you now. Click to see his profile"; 
      i.putExtra("Notification", data.get("other.name") + " is following you now. Click to see his profile"); 
      break; 

     default: 
      i = null; 
      break; 
    } 

    if (i != null) { 
     TaskStackBuilder stack = TaskStackBuilder.create(this); 

     if(noActivities){ 
      stack.addParentStack(Photostream.class); 
     } 

     stack.addNextIntent(i); 

     PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("PIC LOC") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSound) 
       .setContentIntent(pendingIntent); 


     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 
    } 

} 

更新2:所以搜索了很多,我發現,我missunderstood堆棧生成器的工作原理之後。我發現另一個線程,他們描述瞭如何添加工作。 Editing the Manifest in order to have a previous stack。 我快和你跳給我提供了非常和善教程的一部分......

感謝您的幫助球員;)

回答

1

創建此類通知時,您應該製作stack builder

Intent resultIntent = new Intent(this, NotificationTapActivity.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
// Adds the back stack 
stackBuilder.addParentStack(MainActivity.class); 
// Adds the Intent to the top of the stack 
stackBuilder.addNextIntent(resultIntent); 
// Gets a PendingIntent containing the entire back stack 
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
PendingIntent.FLAG_UPDATE_CURRENT); 

因此,無論何時用戶將點擊通知,MainActivity將被插入堆棧。

另一個解決方案,您可以在其中處理NotoificationTapActivity的後按。您可以在其中檢查堆棧中是否存在註釋,然後您可以完成當前活動並從那裏啓動MainActivity。

0

你可以簡單地處理,在通知活動的onBackPressed。

隨着國旗的幫助下,你可以知道你的通知活動是通過通知點擊打開,然後在onBackPressed去你mainactivity如下:

Intent intent =new Intent(context,MainActivity.class) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

0
/**Just use below code inside onMessageReceived()**/ 

PendingIntent pendingIntent; 
    Uri defaultSoundUri; 
    Intent notification_intent; 

String message = "Your Message"; 

notification_intent = new Intent(this, YourActivity.class); 
notification_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

pendingIntent = PendingIntent.getActivity(this, 0 , notification_intent, PendingIntent.FLAG_ONE_SHOT); 
     defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(getNotificationIcon(notificationBuilder)) 
       .setContentTitle(this.getResources().getString(R.string.app_name)) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) 
       .setContentIntent(pendingIntent); 

    String[] multilineDescription = new String[]{message}; 

    message = multilineDescription[0]; 

    for (int i=1; i<multilineDescription.length; i++) 
    { 
     message += System.getProperty("line.separator"); 
     message += multilineDescription[i]; 
    } 

    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); 

    /***Above commented is for strecting the big message in Push Notification******/ 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify((int)MESSAGE_NOTIFICATION_ID /* ID of notification */, notificationBuilder.build()); 
1

您應該使用TaskStackBuilder。這是最有效的方式,因此開發了這種結構。

我用百達時TaskStackBuilderPush Notifications包括我的項目。

重寫onBackPress()是不是好方法,因爲它需要在你的應用程序結構複雜,你需要處理很多事情。

檢查this教程。

0
Intent intent = getIntent();     
String data= intent.getStringExtra("from");            
if(data.equalsIgnoreCase("notificaton")           
{ 

// call Mainactivity 

}                    
else if(){} 
0

PendingIntent可以實現多重意圖。只需創建一個陣列意圖

Intent[] intents = new Intent[2]; 
    Intent intent1 = new Intent(this, MainActivity.class); 
    Intent intent2 = new Intent(this, YourActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, intents,PendingIntent.FLAG_ONE_SHOT); 

而當您完成YouActivity時,它將返回到MainActivity。

相關問題