Search code examples
androidbroadcastreceiverandroid-broadcastreceiver

Context not passing to NotificationReceiver (BroadcastReceiver)


I have a notification that when I tap on it, it simply closes, the app is not brought back into view.

This is in my MainActivity -

Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class); intent.putExtra("Message", notificationText);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Then the NotificationReceiver looks like this -

public class NotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        String notificationText = intent.getStringExtra("Message");
        //if we want ring on notification then uncomment below line
//        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.rr)
                .setContentTitle("Check your reminders!")
                .setContentText(notificationText)
                .setAutoCancel(true);

        notificationManager.notify(100, builder.build());

    }
}

In my Manifest I have this.

<receiver
            android:name=".NotificationReceiver" />

What am I missing?

Thanks!


Solution

  • You should create new Intent to open the activity, instead of existing intent which is coming from onReceive.

        public class NotificationReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    
    
                String notificationText = intent.getStringExtra("Message");
                //if we want ring on notification then uncomment below line
    //        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
                Intent resultIntent = new Intent(context, MainActivity.class);
                resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
                NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.drawable.rr)
                        .setContentTitle("Check your reminders!")
                        .setContentText(notificationText)
                        .setAutoCancel(true);
    
                notificationManager.notify(100, builder.build());
    
            }
        }