Android,点击通知组

Yak*_*mer 10 notifications android android-pendingintent

我在 Android 应用中有一组通知。我需要我的应用程序知道当用户单击通知时应用程序是否已打开。我使用了一个待定的意图,它的效果非常好。但是如何检查用户是否点击了通知组?它没有显示链接,没有通知声音,什么也没有。我在文档中没有找到任何信息。请帮忙。在此处输入图片说明

Was*_*mon -2

您可以在意图中添加标志,例如isFromNotification布尔值,然后将该意图传递给挂起的意图,然后通知用户。

现在,在活动检查意图中有此参数,则用户来自通知。(就这么简单)

示例代码

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());
Run Code Online (Sandbox Code Playgroud)