PendingIntent.getBroadcast не работает в Oreo - PullRequest
0 голосов
/ 07 февраля 2019

У меня уже есть приложение в 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>

1 Ответ

0 голосов
/ 08 февраля 2019

Начиная с Android 8 (Oreo), вы больше не можете зарегистрировать BroadcastReceiver для неявного Intent в манифесте.Вот что вы делаете с этим:

<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 для Notification, следующим образом:

    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 ипоиск "Ограничения на вещание"

...