Search code examples
androidnotificationspush-notificationalarmmanagerandroid-alarms

Get message of notification and send to the activity


Here is i am creating notification with the message "wake up wake up !!!" What i want to do when notification comes on particular time and user clicks on it i need to get that message "wake up wake up !!!" and send to SnoozeActivity where i am creating custom alert dialog and i want to show that message into that dialog

        alarmNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
       PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, SnoozeActivity.class), 0);


        NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
                this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("wake up wake up !!!"))
                .setContentText(msg).setAutoCancel(true);


        alamNotificationBuilder.setContentIntent(contentIntent);
        alarmNotificationManager.cancelAll();
        alarmNotificationManager.notify(1, alamNotificationBuilder.build());

But i am not able to figure out how to get the message/data of clicked notification and send to next activity?


Solution

  • An Intent can carry data which you attach via Intent.putExtra()like this:

    Intent intent = new Intent(this, SnoozeActivity.class);
    intent.putExtra("com.example.mycoolapp.MYDATA", "wake up wake up !!!");
    

    Then you continue with

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    In the onCreate() method of your SnoozeActivity, you can query the Intent for extras:

    Intent intent = getIntent();
    if (intent.hasExtra("com.example.mycoolapp.MYDATA"))
    {
         String myText = intent.getStringExtra("com.example.mycoolapp.MYDATA");
    }    
    

    It's recommended that the keys you use for Intent extras start with your package name.

    NOTE not every kind of data is possible with putExtra(), please see the documentation