LocalNotification with AlarmManager and BroadcastReceiver not firing up in Android O (oreo)

前端 未结 4 1821
无人共我
无人共我 2020-12-01 06:29

I\'ve got my local notifications running on androids prior to SDK 26

But in a Android O I\'ve got the following warning, and the broadcast receiver

4条回答
  •  孤独总比滥情好
    2020-12-01 07:10

    Create the AlarmManager by defining an explicit intent (explicitly define the class name of the broadcast receiver):

    private static PendingIntent getReminderReceiverIntent(Context context) {
        Intent intent = new Intent("your_package_name.ReminderReceiver");
        // create an explicit intent by defining a class
        intent.setClass(context, ReminderReceiver.class);
    
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }
    

    Also do not forget to create a notification channel for Android Oreo (API 26) when creating the actual notification:

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (VERSION.SDK_INT >= VERSION_CODES.O) {
        notificationManager.createNotificationChannel(NotificationFactory.createNotificationChannel(context));
    } else {
        notificationManager.notify(NotificationsHelper.NOTIFICATION_ID_REMINDER, notificationBuilder.build());
    }
    

提交回复
热议问题