【问题标题】:Android Firebase notification not appear in android version 8 or higher [duplicate]Android Firebase 通知未出现在 android 版本 8 或更高版本中[重复]
【发布时间】:2019-04-02 08:13:45
【问题描述】:

通知在 android 8 或更高版本中不显示,但在 android 8 更低版本中可以正常工作

这就是我尝试的方法

String code,code1,code2,title,message,image,urlImage;
@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        kode = remoteMessage.getData().get("code");
        kode1 = remoteMessage.getData().get("code1");
        kode2 = remoteMessage.getData().get("code2");
        title = remoteMessage.getData().get("title");
        message = remoteMessage.getData().get("message");
        image = remoteMessage.getData().get("image");
        urlImage = remoteMessage.getData().get("image");


        sendNotification_1(title,message, bitmap, code);

}


private void sendNotification_1(String title,String messageBody, Bitmap image, String kode){
        Intent intent = new Intent(this, Home.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("directto", kode);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if(image != null){
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setLargeIcon(convertToBitmap())/*Notification icon image*/
                    .setSmallIcon(R.drawable.logostar)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(image))
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            NotificationManager notificationManager =
                    (NotificationManager) 
            getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0 /* ID of notification */, 
            notificationBuilder.build());
        }
    }

以上是我的代码,在 android 8 更高版本中仍然无法使用

按照大家的建议,在我编辑的代码下方 但还是不行

【问题讨论】:

    标签: java android firebase android-8.0-oreo


    【解决方案1】:

    需要通知渠道

    NotificationChannel mChannel = null;
    
    
    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(context.getString(R.string.alarm_notification));
                mChannel.enableLights(true);
                mChannel.setLightColor(Color.RED);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                mNotificationManager.createNotificationChannel(mChannel);
            } else{
    }
    

    【讨论】:

    • 我已经添加了你的代码,但是还是不行,
    • 我已经添加了你的代码,结果你可以在我上面编辑的问题中看到,问题是,它仍然没有显示任何通知
    【解决方案2】:

    从 android8 开始,需要一个与所有通知关联的频道。

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
            {
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.enableVibration(true);
                notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                assert mNotificationManager != null;
                mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
                mNotificationManager.createNotificationChannel(notificationChannel);
    }
    

    【讨论】:

    • 我已经添加了你的代码,结果你可以在我上面编辑的问题中看到,问题是,它仍然没有显示任何通知
    • notificationBuilder.build()); notificationManager.notify(0 /* 通知ID */, 请互换这两行
    • 是的,我当然添加了它,但图像可能不完整
    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多