【问题标题】:Notification not showing in samsung oreo devices三星奥利奥设备中未显示通知
【发布时间】:2019-03-07 08:25:34
【问题描述】:

我今天遇到了一个奇怪的问题,推送通知在装有 oreo 的三星设备上不显示,但在其他 oreo 设备上工作正常。这是我正在使用的代码:

 private void sendNotification(String messageTitle,String messageBody) {

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder=null;
        try {
        Intent intent = new Intent(this, SplashActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        String NOTIFICATION_CHANNEL="NB Shop Notification";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                .setSound(defaultSoundUri)
                .setChannelId(NOTIFICATION_CHANNEL)
                .setContentIntent(pendingIntent);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            /* Create or update. */
            NotificationChannel channel;
            channel=new NotificationChannel(NOTIFICATION_CHANNEL,
                    NOTIFICATION_CHANNEL,
                    NotificationManager.IMPORTANCE_DEFAULT);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
            notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
        }catch (Exception e)
        {
            Log.d("sendNotification",e.toString());
        }
    }

它没有在日志中显示任何错误。

【问题讨论】:

    标签: android push-notification notification-channel


    【解决方案1】:

    我发现了导致问题的问题,不推荐使用只有单个参数的 NotificationCompat.Builder 的构造函数(NotificationCompat.Builder(this)), 为了摆脱弃用,我们必须使用构造函数发送通道 id,所以我更改了代码并将通道 id 传递给构造函数,如下所示:

    NotificationCompat.Builder(this,NOTIFICATION_CHANNEL)
    

    也删除了

    .setChannelId(NOTIFICATION_CHANNEL)
    

    现在这段代码也可以在三星设备上运行。

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多