PendingIntent.getBroadcast在Oreo中不起作用

时间:2019-02-07 12:54:36

标签: android push-notification android-pendingintent

很久以前,我已经在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>

2 个答案:

答案 0 :(得分:1)

从Android 8(Oreo)开始,您不再可以在清单中为隐含BroadcastReceiverIntent注册。这就是您正在做的事情:

<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>

并将用于创建PendingIntent的{​​{1}}的代码更改为此:

Notification

有关更多信息,请参见https://developer.android.com/about/versions/oreo/background并搜索“广播限制”

答案 1 :(得分:1)

修改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;
}