从通知启动活动后返回主要活动

时间:2017-07-12 10:50:43

标签: android android-intent android-notifications

我从我收到的activity发起了notification。如果按下notification,则会启动activity。如果activities上只有back-stack,或者只有某个activity,我想要移除某个activity并将我的主intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)插入其中,而不是新的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()); } } 活动。

我发现了这个Thread,但我不明白他是如何处理它的两个意图和标志。

即。 public class A { private String s; private Function<String, String> f = e -> s; public A(String s) { this.s = s; } }

以他做的方式做到这一点是否明智,还是应该为此编辑活动堆栈?

我对android dev很新,所以一些建议可以帮助我。 非常感谢;)

更新:所以我选择了堆栈生成器,但不知怎的,它没有设置正确...我没有找到我的错误,我的布尔noActivity肯定会被设置,但我想我不明白我误解了堆栈实际上是如何放置的之前的活动。

s

更新2:所以在搜索了很多之后我发现我很想念堆栈构建器是如何工作的。我找到了另一个线程,他们描述了添加的工作原理。 Editing the Manifest in order to have a previous stack。 我很快就跳过你提供给我的教程的一部分......

感谢你们的帮助;)

6 个答案:

答案 0 :(得分:1)

/**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 :(得分:1)

您应该使用TaskStackBuilder。这是最有效的方法,并且TaskStackBuilder结构就是出于这个原因而开发的。

TaskStackBuilder包含在我的项目中时,我总是使用Push Notifications

覆盖onBackPress()并不是一个好方法,因为它需要在您的应用中使用复杂的结构,并且您需要处理很多事情。

检查this教程。

答案 2 :(得分: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。

答案 3 :(得分:0)

您可以在通知活动的onBackPressed中处理该问题。

在旗帜的帮助下,您可能知道您的通知活动是通过点击通知打开的,然后在onBackPressed中转到您的主要活动,如下所示:

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

答案 4 :(得分:0)

SELECT *
FROM   table1 a
WHERE  EXISTS (SELECT 1
               FROM   table1 b
               WHERE  a.sender = b.receiver
                      AND a.receiver = b.sender)
       AND NOT ( 'a' IN ( sender, receiver ) AND 'b' IN ( sender, receiver ) ) 

答案 5 :(得分:0)

PendingIntent可以实现多重意图。只需创建一个数组意图

/* Add wingRight */
span.next_nav {
    background-image: url(https://www.petasos.be/wp-content/uploads/2017/05/wingRight.png);
    background-repeat: no-repeat;
    background-position: 40% 40%;
}

/* Add wingLeft */
span.prev_nav {
    background-image: url(https://www.petasos.be/wp-content/uploads/2017/05/wingLeft.png);
    background-repeat: no-repeat;
    background-position: 40% 40%;
}

/* Hide fa-icons */
.carousel-control i {
    display: none;  
}

/* No border */
.carousel-control .next_nav, .carousel-control .prev_nav {
    border: none;
}

当你完成YouActivity时,它将返回MainActivity。