gpt4 book ai didi

android - 通知未打开 Activity onCLick

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:58 25 4
gpt4 key购买 nike

我想创建一个通知,当点击它时应该打开 Activity 。但是当我点击通知 Activity 时打不开。这是我的代码:

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

Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);

Notification.Builder notification = new Notification.Builder(context)

.setContentTitle("Message Received")
.setSmallIcon(R.drawable.icon)
.setContentText(payload)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

Notification notificationn = notification.getNotification();

notificationManager.notify(0, notificationn);

最佳答案

使用这个:

Notification.Builder notification = new Notification.Builder(context)
.setContentIntent(getDialogPendingIntent(Text, intentName));

private PendingIntent getDialogPendingIntent(String dialogText,
String intentname) {
return PendingIntent.getActivity(
context,
dialogText.hashCode(),
new Intent(ACTION_DIALOG)
.putExtra(Intent.EXTRA_TEXT, dialogText)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setAction(intentname), 0);
}

getDialogPendingIntent(Text, intentName) : intentName=com.yourProject.exrta.yourIntentName

如果需要,您可以更改 addFlagsputExtra


如果使用 Intent Name 的调用不起作用,请将它与类似的类一起使用,并且它必须起作用:

Intent notificationIntent = new Intent(MainActivity.this, TestActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

NotificationManager notificationManager = (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notification = new Notification.Builder(MainActivity.this)
.setContentTitle("Message Received")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent);

Notification notificationn = notification.getNotification();
notificationManager.notify(1, notificationn);

关于android - 通知未打开 Activity onCLick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15985901/

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