zyz82
zyz82

Reputation: 171

Passing Notification's Text and image to another activity on click event

first of all i tried all the answers mentioned in stack overflow, but none worked with my application, i know that it is simple, but i cant figure out why it is not working. so i saw to share with you my code, in order to help me in this. i am running on API 22.

this is the activity which contain the textView to receive the text from the notification.

public class EventDetails extends AppCompatActivity {
public static TextView txtDet;
public String strdet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    onNewIntent(getIntent());
    setContentView(R.layout.activity_event_details);
    txtDet = (TextView) findViewById(R.id.txtDetail);
    Intent MyIntent = getIntent();
    strdet = MyIntent.getStringExtra("txtDetails");

    txtDet.setText(strdet);



}

i used getStringExtra to pass a text when clicking on calendar date and it is working well, so i want to use the same textView for receiving notification text, i dont have a problem in using another textView, and at the same time i want to receive a distinct image for each notification.

and this is the notification activity, i named AlertReceiver

    public class AlertReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {


        int notificationId  = intent.getIntExtra("id", -1);
        switch(notificationId){
            case 1:
                createNotification(context, "title1", "event1", "event of today");
                break;
            case 2:
                createNotification(context, "title2", "event2", "event of today");
                break;
            case 3:
                createNotification(context, "title3", "event3", "event of today");
                break;
            case 4:
                createNotification(context, "title4", "event4", "event of today");
                break;
        }


           }

    public void createNotification(Context context, String msg, String msgText,String msgAlert){

        PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, EventDetails.class), 0);

        NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.not)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText);
        //intent to fire when notification clicked on
       mBuilder.setContentIntent(notificIntent);
        //how the person will be notified
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        //cancel notification when clicked in the taskbar
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

           mNotificationManager.notify(0, mBuilder.build());




    }
}

any Suggestions?

Upvotes: 0

Views: 526

Answers (1)

Raghavendra B
Raghavendra B

Reputation: 441

Inside create notification method add the data to intent. You just specified on click of notification which activity should be called.

Like this:

PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
        new Intent(context, EventDetails.class).putExtra("txtDetails", yourMsg), 0);

Upvotes: 1

Related Questions