gpt4 book ai didi

android - 点击通知后打开应用程序

转载 作者:IT老高 更新时间:2023-10-28 13:05:42 26 4
gpt4 key购买 nike

我的应用中有一条通知,其中包含以下代码:

//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);

}

我的通知触发得很好,但我的问题是,当我在通知中心点击通知时,它并没有启动我的应用程序。

基本上,点击我的通知后,什么都没有发生!我应该怎么做才能在单击我的通知后开始我的主要 Activity 。谢谢。

最佳答案

见下面的代码。我正在使用它,它正在打开我的 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);

关于android - 点击通知后打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13716723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com