【问题标题】:How to check which notification id is clicked?如何检查单击了哪个通知ID?
【发布时间】:2014-03-05 18:51:29
【问题描述】:

我的应用程序正在接收 GCM 通知。我有不同类型的通知,有时用户可以在状态栏中拥有多个通知。但是我需要知道他到底点击了哪一个。在 GCM onMessage 我用

设置它们
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(Integer.parseInt(notification_id), notification);

单击通知后,我需要获取该通知 ID。我很确定这很简单,但我找不到任何相关信息。

这是来自 GCMIntentService 的 onMessage

@Override
protected void onMessage(Context context, Intent data) {
    String content_title;
    String content_text;
    String event_id;
    String content_info;
    String url;
    String match_id;
    // Message from PHP server
    content_title = data.getStringExtra("content_title");
    content_text = data.getStringExtra("content_text");
    content_info = data.getStringExtra("content_info") + "'";
    event_id = data.getStringExtra("event_id");
    match_id = data.getStringExtra("match_id");
    url = data.getStringExtra("url");
    NOTIFICATION_URL = url;

    // Open a new activity called GCMMessageView
    Intent intent = new Intent(this, GCMMessageView.class);
    // Pass data to the new activity
    intent.putExtra("message", content_title);
    intent.putExtra("url", url);
    // Starts the activity on notification click
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Options opts = new Options();
    opts.inDither = true;
    opts.inScaled = false;


    /* Flag for no scalling */
    // Create the notification with a notification builder
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(drawable_small).setLargeIcon(drawable_big)
            .setWhen(System.currentTimeMillis()).setTicker(content_title)
            .setContentTitle(content_title).setContentInfo(content_info)
            .setContentText(content_text).setContentIntent(pIntent)
            .getNotification();
    // Remove the notification on click
    notification.ledARGB = 0xff00ff00;
    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;


    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(Integer.parseInt(match_id), notification);

    try {
        Uri notification2 = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
                notification2);
        r.play();
    } catch (Exception e) {
    }

    {
        // Wake Android Device when notification received
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        final PowerManager.WakeLock mWakelock = pm.newWakeLock(
                PowerManager.FULL_WAKE_LOCK
                        | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
        mWakelock.acquire();

        // Timer before putting Android Device to sleep mode.
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                mWakelock.release();
            }
        };
        timer.schedule(task, 5000);
    }

}

还有点击

String msg;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    if (intent.hasExtra("url"))
        msg = intent.getExtras().getString("url");
    Log.e("URL", msg);
    setContentView(R.layout.activity_main);

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(msg
            + "?device_id="
            + GCMIntentService.DEVICE_REGISTRATION_ID.toString()));
    startActivity(browserIntent);

    // Toast.makeText(getApplicationContext(),
    // GCMIntentService.DEVICE_REGISTRATION_ID, Toast.LENGTH_LONG).show();
}

【问题讨论】:

  • 您可以在包含您的 notificationId 的待处理意图中放置一个键,并签入从 bundle 读取的 id 的目标活动。

标签: java android google-cloud-messaging android-notifications


【解决方案1】:

您可以通过下面的代码,您必须根据您的需要提供通知对象。

    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;

    Intent intent = new Intent(context, NotificationActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("message", YOUR_DATA);

    int requestID = (int) System.currentTimeMillis();
    PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,
            intent, 0);

    if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification(icon, message, when);
        notification.setLatestEventInfo(context, appname, message,
                contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify((int) when, notification);

    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(appname).setWhen(when)
                .setAutoCancel(true).setContentTitle(appname)
                .setContentText(message).build();

        notificationManager.notify((int) when, notification);

    }

当用户点击任何通知时,它将重定向到 NotificationActivity 类。 在此活动中,您可以在 OnCreate() 方法中获取已设置的数据。

 Intent intent = getIntent();
if (intent.hasExtra("message"))
        String msg = intent.getExtras().getString("message");

我认为这会有所帮助。

【讨论】:

  • 谢谢!这真的帮助了我
  • 嗯.. 似乎这不起作用。它每次都会覆盖额外的值。如果我设置一个 id = 1 的通知和第二个 id = 2 的通知,无论我点击哪个通知,它都会返回最后一个 - 2
  • 您可以将您的 id 写入 putExtra 意图参数。根据通知,它是唯一的。
  • Ok..Ok 我弄错了,你忘了加一行,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);在你的代码中..
  • 并从您的 pIntent 中删除 PendingIntent.FLAG_UPDATE_CURRENT 标志。请比较我发布的代码和正在做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多