Android 9 Api 28 Notification not showing

不问归期 提交于 2020-08-11 06:15:30

问题


I'm trying to show notification but it's not working for both Oreo and Pie version.

It's working in kitkat version.

I tried all the possible condition i can do, i don't know what else i'm missing here

Here is my code:

String idChannel = "my_channel_01";
        Intent mainIntent;
        mainIntent = new Intent(ChecklistOptionActivity.this, CashCountOptionActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationChannel mChannel = null;
        // The id of the channel.

        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(context.getString(R.string.app_name))
                .setSmallIcon(R.drawable.bc_icon)
                .setContentIntent(pendingIntent)
                .setContentText("Test");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mChannel = new NotificationChannel(idChannel, context.getString(R.string.app_name), importance);
            // Configure the notification channel.
            mChannel.setDescription("Test");
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mNotificationManager.createNotificationChannel(mChannel);
        } else {
            builder.setContentTitle(context.getString(R.string.app_name))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setColor(ContextCompat.getColor(context, R.color.colorBlue))
                    .setVibrate(new long[]{100, 250})
                    .setLights(Color.YELLOW, 500, 5000)
                    .setAutoCancel(true);
        }
        mNotificationManager.notify(1, builder.build());

回答1:


String channel_id = createNotificationChannel(context); 

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channel_id);

The below method is to generate a new channel_id.

 public static String createNotificationChannel(Context context) {

                // NotificationChannels are required for Notifications on O (API 26) and above.
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                    // The id of the channel.
                    String channelId = "Channel_id";

                    // The user-visible name of the channel.
                    CharSequence channelName = "Application_name";
                    // The user-visible description of the channel.
                    String channelDescription = "Application_name Alert";
                    int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
                    boolean channelEnableVibrate = true;
        //            int channelLockscreenVisibility = Notification.;

                    // Initializes NotificationChannel.
                    NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
                    notificationChannel.setDescription(channelDescription);
                    notificationChannel.enableVibration(channelEnableVibrate);
        //            notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);

                    // Adds NotificationChannel to system. Attempting to create an existing notification
                    // channel with its original values performs no operation, so it's safe to perform the
                    // below sequence.
                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    assert notificationManager != null;
                    notificationManager.createNotificationChannel(notificationChannel);

                    return channelId;
                } else {
                    // Returns null for pre-O (26) devices.
                    return null;
                }
            }
  • Here you will get push notification using channel_id in your device which is consist 26+ SDK version.
  • Because of the NotificationCompat.Builder(context) is a deprecated method now you will use an updated version which is having two parameters one is context, the other is channel_id.
  • NotificationCompat.Builder(context, channel_id) updated method. try it.
  • In the 26+ SDK version of the device, you will create channel_id every time.


来源:https://stackoverflow.com/questions/56323523/android-9-api-28-notification-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!