PendingIntent.getBroadcast 不适合奥利奥

PendingIntent.getBroadcast not working in Oreo

我很久以前就有一个应用程序已经在 playstore 上了,最近通知没有向用户开放

这是我的代码

private void showNotification () {

        PhoneUtils.clearAllNotifications(getApplicationContext());

        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = “app”;
        int notificationId = 100;

        createNotificationChannel(channelId , notificationManager);

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), channelId)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getApplicationContext().getResources().getString(R.string.app_name))
                .setContentText(mAlert)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                .setContentIntent(getOpenNotificationIntent())
                .setDefaults(Notification.DEFAULT_ALL)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .build();

        notificationManager.notify(notificationId, notification);

}

private PendingIntent getOpenNotificationIntent () {

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

        Intent intent = new Intent(“com.app.OPEN”);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_TYPE, mType);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_ID, mId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_DIALOG_ID, mDialogId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_MESSAGE_ID, mMessageId);
        Notification notification = null;

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return pendingIntent;
}

<receiver
    android:name=".fcm.OpenNotificationReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="com.app.OPEN" />
    </intent-filter>
</receiver>

从 Android 8 (Oreo) 开始,您不能再在清单中为 隐式 Intent 注册 BroadcastReceiver。这就是你正在做的事情:

<receiver
    android:name=".fcm.OpenNotificationReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="com.app.OPEN" />
    </intent-filter>
</receiver>

您应该使用 显式 Intent,而不是这样:

将清单条目更改为:

<receiver
    android:name=".fcm.OpenNotificationReceiver">
</receiver>

并将用于为 Notification 创建 PendingIntent 的代码更改为:

    Intent intent = new Intent(this, OpenNotificationReceiver.class);
    intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_TYPE, mType);
    intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_ID, mId);
    intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_DIALOG_ID, mDialogId);
    intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_MESSAGE_ID, mMessageId);
    Notification notification = null;

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

有关详细信息,请参阅 https://developer.android.com/about/versions/oreo/background 并搜索 "Broadcast limitations"

修改getOpenNotificationIntent方法。

private PendingIntent getOpenNotificationIntent () {

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

        Intent intent = new Intent(“com.app.OPEN”);

        //add this line
        intent.setPackage(getApplicationContext().getPackageName());

        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_TYPE, mType);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_ID, mId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_DIALOG_ID, mDialogId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_MESSAGE_ID, mMessageId);
        Notification notification = null;

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return pendingIntent;
}