如何捕获通知点击?

时间:2016-03-24 18:11:43

标签: android notifications push-notification

点击通知后我需要调用一些方法。我的通知有一个pendingIntent,用于打开浏览器。但是在用户点击它之后我还必须做一些其他事情。哦,我必须承认我从GCM收到我的通知(推送)。

Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlString));
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(mBitmap)
                .setContentTitle(title)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentText(body)
                .setAutoCancel(true);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification myNotification = notificationBuilder.build();
        myNotification.setLatestEventInfo(getBaseContext(), title, body, pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(TAG, myNotification);

1 个答案:

答案 0 :(得分:0)

要处理点击通知本身(不是操作),您需要指定ContentIntent,如下所示:

notificationBuilder.setContentIntent(PendingIntent.getActivity(
              context,
              0,
              new Intent(context, YourActivity.class)
                  .putExtra("extra_something", something)
                  .setData(Uri.parse("pass an uri if needed")),
              PendingIntent.FLAG_UPDATE_CURRENT
          ))

希望它有所帮助。