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

前端 未结 4 1822
无人共我
无人共我 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:25

    Today i had the same problem and my notification was not working. I thought Alarm manager is not working in Oreo but the issue was with Notification. In Oreo we need to add Channel id. Please have a look into my new code:

    int notifyID = 1; 
    String CHANNEL_ID = "your_name";// The id of the channel. 
    CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
    // Create a notification and set the notification channel.
    Notification notification = new Notification.Builder(HomeActivity.this)
                .setContentTitle("Your title")
                .setContentText("Your message")
                .setSmallIcon(R.drawable.notification)
                .setChannelId(CHANNEL_ID)
                .build();
    

    Check this solution. It worked like charm.

    https://stackoverflow.com/a/43093261/4698320

    I am showing my method:

    public static void pendingListNotification(Context context, String totalCount) {
            String CHANNEL_ID = "your_name";// The id of the channel.
            CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationCompat.Builder mBuilder;
    
            Intent notificationIntent = new Intent(context, HomeActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(AppConstant.PENDING_NOTIFICATION, AppConstant.TRUE);//PENDING_NOTIFICATION TRUE
            notificationIntent.putExtras(bundle);
    
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            if (android.os.Build.VERSION.SDK_INT >= 26) {
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                mNotificationManager.createNotificationChannel(mChannel);
                mBuilder = new NotificationCompat.Builder(context)
    //                .setContentText("4")
                        .setSmallIcon(R.mipmap.logo)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setLights(Color.RED, 300, 300)
                        .setChannelId(CHANNEL_ID)
                        .setContentTitle(context.getResources().getString(R.string.yankee));
            } else {
                mBuilder = new NotificationCompat.Builder(context)
    //                .setContentText("4")
                        .setSmallIcon(R.mipmap.logo)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setLights(Color.RED, 300, 300)
                        .setContentTitle(context.getResources().getString(R.string.yankee));
            }
    
            mBuilder.setContentIntent(contentIntent);
    
            int defaults = 0;
            defaults = defaults | Notification.DEFAULT_LIGHTS;
            defaults = defaults | Notification.DEFAULT_VIBRATE;
            defaults = defaults | Notification.DEFAULT_SOUND;
    
            mBuilder.setDefaults(defaults);
            mBuilder.setContentText(context.getResources().getString(R.string.you_have) + " " + totalCount + " " + context.getResources().getString(R.string.new_pending_delivery));//You have new pending delivery.
            mBuilder.setAutoCancel(true);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    

提交回复
热议问题