【发布时间】:2016-03-29 20:16:02
【问题描述】:
我使用 GCM,当收到通知时,我想做一些特别的事情。所以,我需要类似onClickListener 的通知。任何想法 ?
这是我的代码:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.lastlast)
.setContentTitle(getString(R.string.message))
.setContentText(resMessage+getString(R.string.interested_int_you))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
【问题讨论】:
-
通知没有 onClickListener 。您必须将 PendingIntent 用于您已经在做的事情。在您的情况下,每当用户单击通知时,都会启动 MainActivity,如果您想在 MainActivity 启动时做不同的事情,您可以在“intent”中输入一些值并检查 MainActivity 中的值。
-
感谢您的解释
标签: android android-intent notifications google-cloud-messaging onclicklistener