gpt4 book ai didi

Android,点击通知组

转载 作者:太空狗 更新时间:2023-10-29 14:37:31 25 4
gpt4 key购买 nike

我在 Android 应用程序中有一组通知。我需要我的应用程序知道当用户单击通知时该应用程序是否已打开。我使用了一个未决的 Intent ,它的效果非常好。但是我如何检查用户是否点击了通知组?它没有显示任何链接,没有通知声音,什么都没有。我在文档中找不到任何信息。请帮忙。 enter image description here

最佳答案

您可以在 Intent 中添加标志,例如 isFromNotification bool 值,然后将该 Intent 传递给未决 Intent ,然后通知用户。

现在在 Activity 检查 Intent 中有这个参数然后用户来自通知。(就这么简单)

示例代码

Intent notificationIntent = new Intent(this, TargetActivity.class);

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);

String channel_Id = "app_channel_01";

notificationIntent.putExtra(AppConstants.INTENTDATA.ISFROMNOTIFICATION,
true);

PendingIntent pendingIntent = PendingIntent.getActivity(MyApplication.getInstance(),
0, notificationIntent,
0);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyApplication.getInstance(), channel_Id)
.setSmallIcon(R.mipmap.ic_launcher)
.setColor(MyApplication.getInstance().getResources().getColor(android.R.color.white))
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis());

NotificationManager notificationManager =
(NotificationManager) MyApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

// Create or update.
NotificationChannel channel = new NotificationChannel(channel_Id,
"General Notifications",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(notificationId, notificationBuilder.build());

关于Android,点击通知组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493037/

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