0

I need to open a specific activity when the app is running in background (not killed) using Firebase notification system. As per the doc, Notification tray will open the default activity and send the intent to it.

To open a specific activity, I added an <Intent> to Manifest file:

 <intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Sending data through Postman as a Post request:

{ 
  "notification" : {
      "OPEN_ACTIVITY_1":".activity.TemActivity_",
      "body" : "update !"
   }, 
  "data": {
      "type":0,
      "question":"open the activity"
 },
 "to": "dDmXZa9k6-o:APA91bEx91oyYnOyzK3kX839EqbvA2CgY74s-
fLUs8MCYZzVmnp-RpgyI3zicEFVS37K7A4kPpbbqYU9hMBEbs-
PrBXlSTYTOBVd7O3CNvl1vaTEbGIHjLLRTWvEK2VRCvJkhQTXWtr3"
}

However, I am getting the notificaiton and when I am clicking the notificaiton tray. It's always opening the default intent activity. What Am I doing wrong here to open the TeamActivity_ (Using android annotation)

Edit-1

public class MessagingService extends FirebaseMessagingService {

public String TAG = "Data";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    System.out.println("data");
  }
}

My token is valid because I can see the notification tray.

Community
  • 1
  • 1
komal sharma
  • 195
  • 1
  • 3
  • 12
  • will you please share your java code of firebase messaging – Ashish Pandey Jul 07 '17 at 07:50
  • @AshishPandey Firebase messaging code for what? Are you asking about `onMessageReceived`? – komal sharma Jul 07 '17 at 07:52
  • yes just post the whole code you have written in that class – Ashish Pandey Jul 07 '17 at 07:58
  • Let me post it but it has nothing to do with "Getting notification when the app is getting killed". Debug will get there only when the app is open – komal sharma Jul 07 '17 at 07:59
  • Possibly helpful (if not a duplicate) post [here](https://stackoverflow.com/q/40181654/4625829) – AL. Jul 07 '17 at 08:52
  • @AL.Thanks for the reference but the answer there is just telling the way to accept the data from intent (Which is working for me). I am not able to open a specific activity when the notification tray is getting tapped – komal sharma Jul 07 '17 at 09:02
  • @komalsharma see my answer hope this will give you some idea how to achieve it. – Andy Developer Jul 07 '17 at 09:37
  • @AndyDeveloper Your code will only work when the app is in foreground. I am looking for a way when the app is in background. – komal sharma Jul 07 '17 at 09:43
  • I am using this code even if the app is in background or foreground this will generate the notification in tray for me. – Andy Developer Jul 07 '17 at 09:46
  • @AndyDeveloper It will generate the notification tray but what next. I am asking that I have to open a specific activity intent not the default intent when app is in background. What you are saying is correct but not the complete answer – komal sharma Jul 07 '17 at 10:06
  • @komalsharma whenever the notification come from Firebase it always call the onMessageReceived() method inside this method you just need to check the if(condition) { Intent notificationIntent1 = new Intent(this, TempActivity.class); notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(notificationIntent1); }. I don't know where you are facing the problem but this works for me everytime since I was used the GCM and still in FCM it works like a charm. – Andy Developer Jul 07 '17 at 10:12
  • @Andy Checkout my answer. – Amit Pal Jul 07 '17 at 10:26
  • @AmitPal Cool it works! – komal sharma Jul 07 '17 at 10:28
  • @AmitPal I didn't see anything new. This is the same post which is the question. She already said that she added intent-filter so what's new in this post. Anyways :) – Andy Developer Jul 07 '17 at 10:33
  • @AndyDeveloper I think you missed a bug in it. Have a look at the code and you will that she is not using "Click_action" in `post` request. Be precisely ` "OPEN_ACTIVITY_1":".activity.TemActivity_",` should be ` "click_action":"OPEN_ACTIVITY_1",`. Got it? – Amit Pal Jul 07 '17 at 10:35
  • @AndyDeveloper Your answer will be perfect for "Foreground" not the "Background". – Amit Pal Jul 07 '17 at 10:38
  • I was thought it is a payload. If you parse that payload without using intent filter in that case my answer is perfectly working. – Andy Developer Jul 07 '17 at 10:40
  • @AndyDeveloper It will not even work in that case dude!. Let me tell you what will happen in your scenario. Your application is running in the background. A new notification will come and when you click on that notification tray `Nothing will change`. The reason: Because `FirebaseMessagingService ` is only capable of accepting notification when the app is in the foreground. – Amit Pal Jul 07 '17 at 12:38

1 Answers1

0

Let me give you a simple example.

In AndroidManifest (Let's say you want to open SampleActivity_)

    <activity android:name=".activity.SampleActivity_"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@style/AppTheme">

            <intent-filter>
                <action android:name="OPEN_ACTIVITY_1"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

Now make a Post request

{ 
 "notification" : {
       "click_action":"OPEN_ACTIVITY_1",
       "body" : "new Symulti update !"
 }, 
  "data": {
      "type":0,
      "question":"what are you doing?"
},
   "to": "cfXBKN_vhFw:APA91bH268KeCeFLlp0OHH-
          UNPgR_njeNmJET0PvrcrHe6VLN-
          w6UQCCO_LbN0TeNhpjqDDStNnt4t0FGQ6S5puz49QSdsK1BKb8R7Q1uYNpP_
          UqsRIETuOh1TRst
          JAsfJS7VI7Atpfx"
}

I have tested it and working perfectly!

Only when app is running in background

Amit Pal
  • 10,604
  • 26
  • 80
  • 160