上下文未传递给 NotificationReceiver (BroadcastReceiver)

Context not passing to NotificationReceiver (BroadcastReceiver)

我收到一条通知,当我点击它时,它只是关闭,应用程序不会重新出现在视图中。

这是在我的 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);

然后 NotificationReceiver 看起来像这样 -

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());

    }
}

在我的清单中有这个。

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

我错过了什么?

谢谢!

您应该创建新的 Intent 来打开 activity,而不是来自 onReceive 的现有 Intent。

    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());

        }
    }