通知 - 正在运行的启动活动或片段

时间:2014-11-20 12:23:26

标签: android android-fragments notifications android-notifications

在我的应用中,我目前正在显示本地通知,工作正常。

我有一个与多个片段相关联的单个活动,Say MainActivity与A,B,C片段相关联。

因此,当通知出现时,我将最小化应用程序并点击通知,那时我希望应用程序从我离开的地方打开(最小化)。

假如没有任何内容或说应用程序被杀死,那么请重新打开应用程序。

我们如何实现这一目标?

我这里没有发生的是我的代码:

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

                            Intent MyIntent = new Intent();
                            MyIntent.setClassName("com.my.app.", "com.my.app.MainActivity");
                            MyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            MyIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                            MyIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

                            PendingIntent StartIntent = PendingIntent.getActivity(mContext,0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT | Notification.FLAG_AUTO_CANCEL);

                            Notification mNotification = new Notification.Builder(mContext)
                            .setTicker("My App")
                            .setContentTitle("My App")
                            .setContentText("Thank you for tap to go back!!!")
                            .setSmallIcon(R.drawable.notification)
                            .setContentIntent(StartIntent)
                            .build();
                            int NOTIFICATION_ID = 1;
                            notificationManager.notify(NOTIFICATION_ID , mNotification);  

1 个答案:

答案 0 :(得分:0)

试用此代码

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

                                    Intent MyIntent = new Intent();
                                    MyIntent.setClassName("com.my.app.", "com.my.app.MainActivity");
                                    MyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
                                    PendingIntent StartIntent = PendingIntent.getActivity(mContext,0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT | Notification.FLAG_AUTO_CANCEL);

                                    Notification mNotification = new Notification.Builder(mContext)
                                    .setTicker("My App")
                                    .setContentTitle("My App")
                                    .setContentText("Thank you for tap to go back!!!")
                                    .setSmallIcon(R.drawable.notification)
                                    .setContentIntent(StartIntent)
                                    .build();
                                    int NOTIFICATION_ID = 1;
                                    notificationManager.notify(NOTIFICATION_ID , mNotification);