2

I've implemented FCM notification system in my app, my problem is which it works fine only if application is running or in background, but if it is killed notifications do not appear, i've this code

Manifest.xml
 <service android:name="com.Ryuk.listenme.FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
</service>
______________________________________________________________

myServer.php

function notify($tokens,$message)
{

$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
              'registration_ids' => $tokens,
              'data' => $message         
              );
$headers = array(
                'Authorization:key = mycode',
                'Content-Type: application/json'
                );

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);
   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
   }
   curl_close($ch);
   return $result;
}
______________________________________________________________

FirebaseMessagingServer.java //which works only in running/background

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

    showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message)
{
    Intent i = new Intent ();
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Test FCM")
            .setContentText(message)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)

            .setContentIntent(pendingIntent);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());

}
}

I dont' know if problem is in my application or in my message format (in php), I tried so many solutions on google but they were realistic a year ago and they did not work

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is this when the app is force closed, or just in the background? – Shmuel Apr 28 '17 at 12:34
  • @Federico De Luca : Check(Log) you will get message onMessageReceived or not. – Janak Apr 28 '17 at 12:56
  • 1
    I recently struggled with this. Sometimes I will receive the notifications, but sometimes I won't. Some people even speculate that Firebase whitelist's application like Whatsapp to give notifications even when they're turned off via the system tray.http://stackoverflow.com/a/39505298/4653908. There is very little documentation about this problem. – Guido Apr 28 '17 at 13:19
  • @guido I find it very hard to believe that GCM/FCM treats apps differently. – Shmuel Apr 28 '17 at 14:10
  • @Shmuel it happens only when app is force closed – Federico De Luca Apr 28 '17 at 14:48
  • @Janak : onMessageReceived is not called if app is closed (log not appear)? – Federico De Luca Apr 28 '17 at 14:49
  • @FedericoDeLuca : Check Your PHP code. – Janak Apr 28 '17 at 19:22
  • @Shmuel It works very inconsistent though. I can't seem to find a reason why it's happening. – Guido May 01 '17 at 06:36
  • *Just adding a supporting comment to what @Guido's pointing out* -- GCM/FCM is not treating apps differently. It's the different devices themselves that treat push notifications differently. Cheers! :) – AL. May 02 '17 at 03:36
  • @AL. so how is it possible that I see notifications from Whatsapp when it is force closed? If it is not because of GCM/FCM whiteliste it means that some other app is showing the notification on behalf of Whatsapp? – lelloman Aug 18 '17 at 10:29
  • @lelloman See the answers in [this post](https://stackoverflow.com/q/39504805/4625829) – AL. Aug 18 '17 at 10:32
  • @AL. I still don't understand one thing. my app is force closed (from settings). can I receive a push? how the hell is Whatsapp doing it? – lelloman Aug 18 '17 at 10:40
  • @lelloman If you read my answer in the post I linked, I mentioned "*the device manufacturers have whitelisted most of the well-known apps*". – AL. Aug 18 '17 at 10:51
  • @AL. Thanks for the answer, I also believe this is the case. But I'm wondering, shouldn't that be GCM/FCM that is whitelisting apps? If you force close an app, the push message is still delivered to GCM/FCM on the device, it's just that the push is not forwarded to your app...? – lelloman Aug 18 '17 at 10:57
  • If the device itself doesn't want to receive it, regardless of whatever service it is, it won't be received. – AL. Aug 18 '17 at 10:59

3 Answers3

2

Once an app is force closed you will not receive background intents. This includes GCM/FCM messages.

"Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications."

"Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stopped applications." https://developer.android.com/about/versions/android-3.1.html#launchcontrols

GCM push notification works after app Force Stop?

Fun fact, Samsung devices often kill background apps with an "Optimize Apps" feature. This may affect your app. See here for more info - Samsung "App optimisation" feature kills background applications after 3 days

Shmuel
  • 3,916
  • 2
  • 27
  • 45
1

According to docs.

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload. In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

When you are in background, FCM will show notification in system tray based on the info from notification payload. Title, message, and icon are fetched from the notification payload. For more refer to this question.

Community
  • 1
  • 1
karanatwal.github.io
  • 3,613
  • 3
  • 25
  • 57
  • Thanks, but how i can do this? I have to edit my php code? Sorry, maybe i'm noob, but i don't understand where i've to put the payload code... – Federico De Luca Apr 28 '17 at 14:30
  • 1
    this isn't helpful not does it address the question. it's clearly stated that the issue is not when the app is running in the background but rather when it's totally closed (not running at all) – takecare Nov 16 '17 at 17:26
0

I've solved it, it was a setting in my phone which prevents notifications