2

I have implemented GCM push notification :

Case 1 : When app is in foreground receive notification on all devices - OK

Case 2 : when app is in background receive notification on all devices - OK

Case 3 : when app is closed from task manager or recently app list won't receive notification on some devices and receive on some devices.

so can anyone help me how can we achieve to receive notification on all devices when app is closed

this my code snippet for GCM :

How to open android app on all devices when receive push notification (GCM) while app is closed?

Community
  • 1
  • 1
Prashant
  • 66
  • 9
  • 1
    you can not do much about that. Custom ROM device use different rule for app notification. such as REDMI devices. – Divyesh Patel May 17 '17 at 12:50
  • @DivyeshPatel then how whatsapp ,facebook apps working even if they are closed.? – Prashant May 17 '17 at 12:53
  • GCM is deprecated, use FCM instead. – Haris Qurashi May 17 '17 at 12:54
  • because, they are white listed in Devices due to their popularity. – Divyesh Patel May 17 '17 at 12:54
  • @HarisQureshi R you sure after FCM is will fet notification even app is closed? thanx – Prashant May 17 '17 at 12:55
  • @DivyeshPatel OK thank you. :) – Prashant May 17 '17 at 12:56
  • No you need to code manually to open your app, FCM is enhanced and far more batter than GCM. Best way is to generate a notification using `NotificationManager` and when user taps on notification open your app. – Haris Qurashi May 17 '17 at 12:56
  • @HarisQureshi yes you r right.. i have done how to open app..but main problem is that when my app is close m not receiving any push notification.is it get working after FCM that what i asking you ? – Prashant May 17 '17 at 12:58
  • in my device, if i remove app from recents apps, it force stops immediately. Even whatsapp. so after some time whatsapp shows notification to open whatsapp to show new messages even if there is no new messages. – Divyesh Patel May 17 '17 at 12:58

2 Answers2

0

GCM? Not FCM? Google recommends using FCM - so this answer might not be applicable to all aspects but I want to share what I know anyway.

What happens on a device depends on the content of the notification.

Basically you have two types of Notifications:

  • If the json of the message does NOT contain a "notification" object (which describes the visible part of the notification in the top toolbar of the device), your App will always receive the notification (wake up), no matter whether it is running, in foreground or background or charging or screen on or off or any other state you can think of. As long as it is connected to the internet of course. Important is here, that you have registered your service correctly in the manifest and with FCM. (You need a Service derived from FirebaseMessagingService that overrides the method onMessageReceived). I can provide a sample if needed.

  • If the json does contain a "notification" object, there can happen two different things:

First, if your app is not running, it will not be started. Instead, just a notification is displayed on the top bar of your device and only when the user clicks it, your app starts.

Second, if your app is running, the Activity that contains the matching intent-filter for the notification will be launched/called.

Here is an example of the json of such a message - notice the tag - This will group similar messages, and notice the click_action this is the Action of the intent for the intent filter that will trigger.

{
"to":"<<FCMToken>>",
"priority":"high",
"notification":{
    "title":"sender name",
    "body":"chat message",
    "icon":"icon_nav_main_chat",
    "tag":"XMPP_MESSAGE",
    "click_action":"XMPP_MESSAGE",
    "sound":"default"
},
"data":{
    "body":"stanza-content"
}
}

So basically, you decide what will happen by sending the appropriate message. If you want to receive it ALWAYS (app running or not), you could omit the notification part, then the message will always be delivered to your service.

The drawback of this is, you have to launch any visible notification locally and put your entire data you need in the payload of the data object. You have about 4000 Bytes here if I remember right.

But there are use cases where this makes sense. Whatsapp does it that way as far as i know, as they use the way more powerful local notifications with direct reply and that kind of things.

Hope this helps, cheers, Gris

Grisgram
  • 3,105
  • 3
  • 25
  • 42
  • i'm interested in the case where the app has been *forced* to stop (from Settings | Apps) but *still receives a call to onMessageReceived()* when a *non-notification* type of message is sent to it through Firebase Messaging. In my own experiments, if the app is truly dead (hard-stopped) Firebase does not call my app's onMessageReceived() *at all.* I'm probably doing something wrong. Any details you can provide that make it work in this particular edge case would be really helpful to answering the original poster's question, I think. This edge case is at the heart of the question. – albert c braun May 17 '17 at 15:13
  • To analyze that I need to see some code. We have a SIP app in production that receives all notifications, any time. Except you DISABLE the app in system settings. You can't do anything about a disabled app. Everything else works fine through FCM. U need a Firebase project, connect your app, correct sender id, etc etc but I think you are already through the basics, so post some example messages and your service and your manifest – Grisgram May 17 '17 at 16:46
  • I'm seeing this behavior in, for example, the "Firebase Quickstarts for Android" code sample, which can be downloaded within Android Studio from: File | New | Import Sample ... (and then find "Firebase Quickstarts for Android" within the list it populates). It's the 'messaging' module within that project. (I believe that Google hosts that same code on GitHub here: https://github.com/firebase/quickstart-android/tree/master/messaging) In any case, once I send a non-notification msg, onMessageReceived is called *only* if the app has been started up. (i.e. its process is running). – albert c braun May 17 '17 at 19:41
  • This sample message results in onMessageReceived being called for me if the app's process has been started, whether backgrounded or foregrounded, but not if the app was forced to stop: { "to":"", "priority": "normal" } – albert c braun May 17 '17 at 19:47
  • As long as you don't show me your implementation I can not help. – Grisgram May 18 '17 at 11:37
  • I posted a new question with the code I'm running and my test script. If you can take a look at it and see what I'm doing wrong that would be great! Thank you. http://stackoverflow.com/questions/44052289/how-to-ensure-that-the-firebase-messaging-sample-implementation-is-notified-even – albert c braun May 18 '17 at 15:44
0

First have a look at WakefulBroadcastReceiver. After that you need to change your code to:

AndroidManifest.xml

// add permission for wake lock
<uses-permission android:name="android.permission.WAKE_LOCK" />

<receiver
    android:name=".GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="YOUR_APPLICATION_PACKAGE_NAME" />
    </intent-filter>
</receiver>

<service android:name=".YourGcmIntentService" />

Java Code:

public class MyWakefulBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
                YourGcmIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

Note:

GCM is deprecated you should consider FCM.

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28