单击通知后打开应用程序 [英] Open application after clicking on Notification
本文介绍了单击通知后打开应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用中有一条通知,其中包含以下代码:
I have a notification in my app with the following code:
//Notification Start
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.n1;
CharSequence tickerText = "Call Blocker";
long when = System.currentTimeMillis(); //now
Notification notification = new Notification(icon, tickerText, when);
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Context context = getApplicationContext();
CharSequence title = "Call Blocker";
text = "Calls will be blocked while driving";
notification.setLatestEventInfo(context, title, text, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(1, notification);
}
我的通知触发得很好,但我的问题是,当我在通知中心点击通知时,它没有启动我的应用程序.
My notifications fires very well, but my problem is that, when I click on notification in Notification Center, it does not start my app.
基本上,点击我的通知后什么也没有发生!我应该怎么做,以便在单击我的通知后开始我的主要活动.谢谢.
Basically, after clicking on my notification nothing happens! What should I do, in order to start my Main activity after clicking on my notification. Thanks.
推荐答案
见下面的代码.我正在使用它,它正在打开我的 HomeActivity.
See below code. I am using that and it is opening my HomeActivity.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
这篇关于单击通知后打开应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文