Android,点击通知组

标签 android notifications android-pendingintent

我在 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/

相关文章:

android - 电池电量低于40%时可以关闭蓝牙吗?

java - 非法状态异常 : Unable to add several TextViews in a View

安卓 O+ : Some phones seem to lack the option to change sound type for notifications

android - 从 Android 到 Sharepoint 2013 的基本身份验证不起作用

Android onOptionsItemSelected 无法运行

java - 通过 setSound() 设置自定义通知声音(和振动)不起作用

javascript - 当页面中发生有趣的事情时如何照亮浏览器窗口/选项卡?

java - AlarmManager 第二次过早触发 PendingIntent

android - 从 Intent 中获取额外信息(BaseAdapter => BroadcastReceiver)

android - 从通知启动时防止重新创建 Activity Activity