【问题标题】:Push notification issue in Oreo 8.0Oreo 8.0 中的推送通知问题
【发布时间】:2018-02-06 14:39:55
【问题描述】:

将应用更新到 8.1 后,通知未显示,我已修复。现在,待处理的意图没有按预期工作。

收到通知后,如果应用程序在后台,我无法导航到应用程序,如果应用程序已关闭,则无法启动。

private void sendNotify(String messageBody) {
    Intent intent = new Intent();
    intent.setAction(Constants.NOTIFY);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    int uniqueId = (int) System.currentTimeMillis();

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Creates the PendingIntent
        PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        String channelID = "com.myapp.ind.push.ServiceListener";// The id of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(channelID, "MyApp", importance);
        // Create a notification and set the notification channel.
        Notification notification = getNotificationBuilder(messageBody, notifyPendingIntent, defaultSoundUri)
                .setChannelId(channelID)
                .build();

        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
            notificationManager.notify(uniqueId, notification);
        }
    } else if (notificationManager != null) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
            uniqueId /* Request code */,
            intent,
            PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri);
        notificationManager.notify(uniqueId /* ID of notification */,
                notificationBuilder.build());
    }
}

private NotificationCompat.Builder getNotificationBuilder(String messageBody, PendingIntent pendingIntent, Uri defaultSoundUri) {
    return new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
}

编辑:

点击通知时它工作正常,直到 6.0。更新到 8.0 后,它无法在 Google Pixel 设备中运行。它没有打开应用程序或将应用程序置于前台。

【问题讨论】:

  • 尝试将setAction(...pendingIntent);添加到notificationBuilder
  • 我不希望通知中有任何 UI 操作按钮。单击通知时,它应该打开应用程序
  • 从 Oreo 开始,隐式广播不再适用于清单注册的接收器(某些系统广播除外)。将Intent 更改为显式Intent;即,针对您的特定接收器类的一个 - 例如,Intent intent = new Intent(this, YourReceiver.class);。此外,您在 if 块中有 PendingIntent.getActivity() 而不是 PendingIntent.getBroadcast()。输入错误?
  • 作为我实现的一部分,我无法明确添加 Result.class。但是在targetedVersion 23之前也是如此
  • 实际上,该限制仅在您定位 26 或更高版本时才会生效(顺便说一句,Play Store 应用很快就会需要)。我以为我已将其编辑到我的评论中,但显然它没有提交。无论如何,您为什么不能指定您的 Receiver 类?

标签: android push-notification android-pendingintent android-8.0-oreo google-pixel


【解决方案1】:

创建如下所示的接收器类。并注册即清单文件。

public class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null
            && intent.getAction().equals("My Call")) {
            Intent startIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            context.startActivity(startIntent);
        }
    }
}

收到通知后Notification类的以下代码。

private void sendNotification() {
    Intent intent = new Intent(this, Receiver.class);
    intent.setAction("My Call");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    int uniqueId = (int) System.currentTimeMillis();
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
            uniqueId /* Request code */,
            intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
        String channelID = "Your Channel ID";// The id of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(channelID, "My_Name", importance);
        // Create a notification and set the notification channel.
        Notification notification = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri)
                .setChannelId(channelID)
                .build();

        notificationManager.createNotificationChannel(mChannel);
        notificationManager.notify(uniqueId, notification);
    } else if (notificationManager != null) {
        NotificationCompat.Builder notificationBuilder = getNotificationBuilder();
        notificationManager.notify(uniqueId /* ID of notification */,
                notificationBuilder.build());
    }
}

private NotificationCompat.Builder getNotificationBuilder() {
    return new NotificationCompat.Builder(this)
            .setSmallIcon(image)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
}

现在我在所有设备中都收到了通知。以前我也收到了,但由于 Oreo 设备中的 Channel 实现而没有显示在状态栏中。现在完美运行。

【讨论】:

  • 你救了我的一天兄弟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多