5

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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

3 Answers3

9

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"

David Wasser
  • 93,459
  • 16
  • 209
  • 274
1

modify getOpenNotificationIntent method.

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;
}
Saeid Mohammadi
  • 319
  • 1
  • 2
  • 11
0

Having had issues with building for Android 12 - I thought I'd share this...

Android API version 31 (Android S) and above require any PendingIntent to have one of two flags set: FLAG_IMMUTABLE or FLAG_MUTABLE. (Android doc. 20) ServerPingWithAlarmManager.onCreate doesn’t do this, and so is throwing an IllegalArgumentException.

https://discourse.igniterealtime.org/t/android-api-31-requires-mutability-flag-for-pendingintent-serverpingwithalarmmanager-4-4-5-fails/91553

Without the Mutable or Immutable flag set PendingIntent.getBroadcast crashes.