How to check which notification id is clicked?

2019-02-19 06:32发布

My application is receiving GCM notifications. I have different type of notifications and at some point the user can have more than one notification in the status bar. However I need to know which one exactly he clicked on. In the GCM onMessage Im setting them with

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

I need to get that notification_id after the click on the notification. I am pretty sure that's something simple but I couldnt find any info about it.

Here are the onMessage from GCMIntentService

@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);
    }

}

And there`s the on click

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();
}

1条回答
仙女界的扛把子
2楼-- · 2019-02-19 07:11

You can go through the below code, You have to ser Notification object as per your need.

    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);

    }

When user click on any notification, it will re-direct to NotificationActivity class. In this activity, in OnCreate() method you can get your data that is set.

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

I think it will help.

查看更多
登录 后发表回答