1

The problem is that When the App is running then notification is working fine (notification goes to targeted screen ) However tha app state is onPause or closwd then notification on click not open the targeted screen How i can solve it here is my code --->

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String NOTIFICATION_CHANNEL_ID = "Sesame" ;
private  static final   String NOTIFICATION_NAME= "Sesame";
private static int count = 0;
private static final String TAG = MyFirebaseMessagingService.class.getName();

@Override
public void onMessageReceived(RemoteMessage message) {
    super.onMessageReceived(message);

    Map<String, String> data = message.getData();

    String title = data.get("title");
    String msg = data.get("message");
    String deepLink = data.get("deepLink");
    String slug = data.get("slug");

sendMyNotification(title,msg,deepLink,message);


}

private void sendMyNotification(String message, String body, String deeplink, RemoteMessage remoteMessage) {

 Intent intent = new Intent(this,MainActivity.class);
    if (remoteMessage.getData().size() >0){
        if (deeplink.equals("deepLink")){
            intent = new Intent(this, TermsConditionsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Log.i(TAG, "sendMyNotification: Terms ");
        }}


 intent.setPackage(null);
    intent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT |
            PendingIntent.FLAG_ONE_SHOT |
            PendingIntent.FLAG_IMMUTABLE);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_NAME, importance);
        mChannel.setDescription(message);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

        mNotifyManager.createNotificationChannel(mChannel);
    }


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_NAME);
    mBuilder.setContentTitle(message)
            .setContentText(body)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setColor(Color.parseColor("#FFD600"))
            .setContentIntent(pendingIntent)
            .setChannelId(NOTIFICATION_CHANNEL_ID)
            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    mNotifyManager.notify(count, mBuilder.build());
    count++;}}

In my Manifest.xml file -->

    <service android:name=".notifications.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
    </service>

And Target Activity as ->

  <activity android:name=".activities.TermsConditionsActivity" >
        <intent-filter>
            <action android:name="deepLink"></action>
            <category android:name="android.intent.category.DEFAULT"></category>
        </intent-filter>
    </activity>

And in my Target Activity i subscribe the topic

   FirebaseMessaging.getInstance().subscribeToTopic("topic");

Please help me..

Laxmi kant
  • 128
  • 1
  • 11

1 Answers1

0
  1. Why you activity not getting open only when app is in BG :

Reference : Firebase onMessageReceived not called when app in background

This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.

You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.

When the intent is launched you can use the

getIntent().getExtras(); to retrieve a Set that would include any data sent along with the notification message.

For more on notification message see docs.

2: How to get call in onMessageReceived if app in BG

Refrence : Firebase onMessageReceived not called. (read this complete answer)

Your messages, that are sent from somewhere, need to have a data payload and no notification payload, or they won't arrive consistently inside of "onMessageReceived" in your MessagingService.

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36