Android,单击通知组

时间:2018-11-27 05:02:00

标签: android notifications android-pendingintent

我在Android App中有一组通知。 我需要我的应用程序知道用户单击通知时是否打开了该应用程序。 我使用了待定的意图,并且效果很好。但是,如何检查用户是否单击了通知组??它没有显示链接,没有通知音,没有任何声音。我在文档中找不到任何信息。请帮助。enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在意图中添加标志,例如 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());