2

I am using Firebase Cloud Messaging on Android.

  • I can successfully receive notification and data messages when the app is in the foreground.
  • The documentation states that if a notification message is received while the app is in the background then Firebase will send a notification to the system tray on behalf of the app. This is not working for me.
  • If a data message is sent and the app is in the background then the onMessageReceived method is called successfully.
  • The most troubling scenario is when I force stop the application and then try sending a notification or data message. The onMessageReceived method doesn't get called in this situation.
  • I have seen a number of articles on SO discussing the background use case, but I have yet to see any explanation for why no messages are being received when the app is stopped entirely.

I have the following in my manifest:

<service android:name=".MyFirebaseMessagingService">
 <intent-filter>
 <action android:name="com.google.firebase.MESSAGING_EVENT" />
 </intent-filter>
</service>
<service android:name=".MyInstanceIdService">
 <intent-filter>
 <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
 </intent-filter>
</service>

This is what the documentation has to say about extending FirebaseMessagingService:

This is required if you want to do any message handling beyond receiving notifications on apps in the background.

It's not entirely clear what they mean by "message handling beyond receiving notifications on apps in the background". I can only assume that they are talking about handling messages when the app is closed.

Any help would be greatly appreciated.

Raghavendra
2,30328 silver badges30 bronze badges
asked Nov 16, 2016 at 7:22
7
  • it works good.Be sure there is active internet connection in the device. Commented Nov 16, 2016 at 7:27
  • I hope you have seen this conversation github.com/firebase/quickstart-android/issues/41 Commented Nov 16, 2016 at 7:27
  • @Wouter what do u mean by force stop here? Did u stop it in app settings? Commented Nov 16, 2016 at 7:32
  • @Raghavendra Yes, I stopped it in the app settings. I wanted to make sure the app was closed down properly. Commented Nov 16, 2016 at 7:35
  • @Wouter I'm not sure though if we force stop it in app settings we don't get notifications I think. Commented Nov 16, 2016 at 7:37

5 Answers 5

6

If you force stop an app you don't get any notifications until the app is restarted. None of your code will work until the app is restarted.

All services of the app will also be stopped until user manually launches the app again

answered Nov 16, 2016 at 8:10
Sign up to request clarification or add additional context in comments.

2 Comments

This appears to be the case after testing the same scenario with Whatsapp. Google Play Services does not appear to start the service if the app has been stopped. I'm not sure why this isn't possible since you are registering the service in the AndroidManifest and the OS should be able to start it even if the app is stopped.
You need someway to stop the app completely in some cases. To stop all its functionalities to do that "Force Stop" will be used. I hope u get it. BTW: When u hit force stop all the services of the app also will be stopped.
1

To send a message using API, you can use a tool called AdvancedREST Client.

{ "data": {
"image": "https://ibin.co/2t1lLdpfS06F.png",
"message": "Firebase Push Message Using API"
"AnotherActivity": "True" },"to" : "f25gYF3***********************HLI"}
answered Nov 16, 2016 at 8:04

2 Comments

Thanks. I've already been testing with the following: curl --header "Authorization: key=$firebase_token" --header "Content-Type:application/json" fcm.googleapis.com/fcm/send -d "{\"to\": \"device_token\",\"data\": {\"message\":\"A data message\"}}". It works correctly if you don't force stop the app, otherwise nothing appears.
by force stopping you close all the background process that are relative to the application, this is the reason you can't receive notification.
0

The problem is in your backend. See this answer. When the message contains notification object, onMessageReceived is called only when the app is foreground. So you have to use only data object. Eg:

var message = {
 to: user.deviceId, // required
 collapse_key: 'key',
 data: {
 title: 'Hello',
 body: 'Body'
 }
}; 

This works for every scenario.

answered Nov 16, 2016 at 7:51

3 Comments

If we do this can we receive notifications even though if we force stop the app in device?
@ Vallilis I don't think this is the solution. I have been testing data messages with the following: curl --header "Authorization: key=$firebase_token" --header "Content-Type:application/json" fcm.googleapis.com/fcm/send -d "{\"to\": \"device_token\",\"data\": {\"message\":\"A data message\"}}". If I force stop the app in app settings and try sending the message nothing appears. I've also tested the same scenario with Whatsapp. No message notification appears if you force stop Whatsapp. This leads me to believe that Google Play Services isn't actually starting up the service.
this wont work after reboot. but works in case that " your app is in background and you want to get data key/value instead of notification title and body "
0

Add action in your manifest activity like below:

<activity
 android:name=".ActivityFCM"
 android:screenOrientation="sensor"
 android:windowSoftInputMode="stateHidden">
 <intent-filter>
 <action android:name="AnyNameYouWant" />
 <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
</activity>

And add click_action in your sending notification server(my example is PHP)

$notification= array(
 'title' => '快訊',
 "body" => "市場大特價",
 'tickerText' => 'Ticker text here',
 'vibrate' => 1,
 'sound' => 1,
 'largeIcon' => 'large_icon',
 'smallIcon' => 'small_icon',
 'click_action' => 'AnyNameYouWant');
answered Dec 7, 2016 at 6:37

Comments

-2

Use this is onCreate method of your MainActivity

 if (getIntent().getExtras() != null) { 
 // I Wrote here. 
 Intent intent = new Intent(MainActivity.this, NotificationActivity.class); 
 startActivity(intent); 
 } 
answered Dec 8, 2016 at 5:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.