Search code examples
androidpush-notificationandroid-pendingintent

PendingIntent.getBroadcast not working in Oreo


I have an app already on playstore long time ago, recently the notifications is not open with users

this is my code

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>

Solution

  • As of Android 8 (Oreo) you can no longer register a BroadcastReceiver for an implicit Intent in the manifest. That's what you are doing with this:

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

    Instead of this, you should use an explicit Intent as follows:

    Change the manifest entry to this:

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

    and change the code you use to create the PendingIntent for the Notification to this:

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

    For more information see https://developer.android.com/about/versions/oreo/background and search for "Broadcast limitations"