Notification on click doesn’t work when device is inactive for a long time

Description:

Note: notification is received in all cases and on click works in all cases when the device is active.

This issue is critical and occurs when the device is in standby mode for a long time or if the notification is received when the device is shutdown & you restart:

The notification when clicked doesn’t open the app, in fact, nothing happens.

Environment
react-native: 0.62.0
Device: Oneplus 3
Android version: 9

Steps to Reproduce Issue:

This issue usually won’t occur when the device is active. To reproduce you need to change the device settings & also make sure the app is killed.

  1. Settings > system > developer options > background process limit > no background processes
  2. Send a notification from the dashboard
  3. Notification is received on the device
  4. Click on the notification, the app doesn’t open

Anything else:
Note: I am using notification extender service to change the layout of the notification.

Also, I tried pending Intent with reference to this comment

Here’s the code:

import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.onesignal.OSNotificationDisplayedResult;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationReceivedResult;
import java.math.BigInteger;
import android.widget.RemoteViews;
import android.app.PendingIntent;
import android.content.Intent;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

public class NotificationBigStyle extends NotificationExtenderService {
   String channelId = "";
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      OverrideSettings overrideSettings = new OverrideSettings();
      RemoteViews notificationBig = new RemoteViews(getPackageName(), R.layout.notification_big);

   notificationBig.setTextViewText(R.id.notification_title,receivedResult.payload.body);
   notificationBig.setImageViewResource(R.id.image,R.drawable.notification);

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

   Intent intent = new Intent(getApplicationContext(), MainActivity.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
   PendingIntent pendingIntent = PendingIntent.getActivity(this,requestID,intent, PendingIntent.FLAG_UPDATE_CURRENT);

   try {
      JSONObject payload = new JSONObject(receivedResult.payload.rawPayload);
      String channelPayload = payload.getString("chnl");
      JSONObject channel = new JSONObject(channelPayload);
      channelId = channel.getString("id");
   }
   catch (JSONException e) {
       e.printStackTrace();
   }


    overrideSettings.extender = new NotificationCompat.Extender() {
         @Override
         public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
                   builder
                    .setCustomHeadsUpContentView(notificationBig)
                    .setCustomContentView(notificationBig)
                    .setContent(notificationBig)
                    .setCustomBigContentView(notificationBig)
                    .setContentIntent(pendingIntent)
                    .setChannelId(channelId)
                    .setAutoCancel(true);
                   return builder;
         }
      };

      OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
      return true;
   }
}

1 possible answer(s) on “Notification on click doesn’t work when device is inactive for a long time

  1. Hey @jkasten2

    Thanks for your time. I checked with 4.0.4 and tried all the cases possible. For now, everything seems to be working!

    I will confirm in the next 3 days once it is tested thoroughly in Beta with our users.