Inability to transfer data through intent during notification

I searched a lot of questions and answers on stackoverflow. I still can not solve my problem.

I installed the broadcast at a specific time. I get a broadcast in another class, AlarmReceiver.java, which creates a broadcast notification. But I pass the line through the intent, which calls the class "AlarmResponder.java" when the notification is clicked.

The class is being called. I get String data that I went through intent only the first time. Next time I won’t get anything. getextras () gives me null.

If I change the notification identifier in NotificationManager.notify (1, mBuilder.build ()); as NotificationManager.notify (2, mBuilder.build ()); it works.

But it always works only once. Please help me.

 public class AlarmReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            CharSequence reminderText = intent.getCharSequenceExtra("reminderText");
            if (reminderText != null && reminderText != "") {
                Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Ringtone r = RingtoneManager.getRingtone(context, notification);
                r.play();
                //Toast.makeText(context, intent.getCharSequenceExtra("reminderText"), Toast.LENGTH_LONG).show();
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(reminderText)
                        .setContentText("its time buddy");
                // Creates an explicit intent for an Activity in your app
                Intent resultIntent = new Intent(context, AlarmResponder.class);
                resultIntent.putExtra("reminderText", reminderText);
                resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                // The stack builder object will contain an artificial back stack for the
                // started Activity.
                // This ensures that navigating backward from the Activity leads out of
                // your application to the Home screen.
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                // Adds the back stack for the Intent (but not the Intent itself)
                stackBuilder.addParentStack(AlarmResponder.class);
                // Adds the Intent that starts the Activity to the top of the stack
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                // mId allows you to update the notification later on.
                mNotificationManager.notify(1, mBuilder.build());
            }
        }
    }

and class AlarmResponder

public class AlarmResponder extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("reminderText"))
        {
            setContentView(R.layout.alarm_responder);
            TextView reminderText = (TextView) findViewById(R.id.reminderText);
            // extract the extra-data in the Notification
            String msg = extras.getString("reminderText");
            reminderText.setText(msg);
        }
    }
}

}

+4
source share

Source: https://habr.com/ru/post/1527159/


All Articles