PendingIntent does not send Intent extras

前端 未结 3 1274
眼角桃花
眼角桃花 2020-12-02 07:54

My MainActicity starts RefreshService with a Intent which has a boolean extra called isNextWeek.

My

3条回答
  •  执笔经年
    2020-12-02 08:44

    Following code should work:-

    int icon = R.drawable.icon;
    String message = "hello";
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("isNexWeek", true);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, pIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
    

    In MainActivity onCreate:

    if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("isNextWeek")) {
            boolean isNextWeek = getIntent().getExtras().getBoolean("isNextWeek");
    }
    

提交回复
热议问题