0

I am trying to send a push notification to my android device using legacy app server protocol. The notification was sent successful as per the response but I did not receive anything on my android device. I also tried sending a notification using the firebase console and there also it shows successfully sent but again I didn't receive anything.

This problem was discussed in the following answer Firebase Notification not received on device when sent via cURL/PHP but I have not made the mistakes addressed in the answers here.

This is the request I made

 curl -X POST 
-H "Authorization:key=MyAuthorizationKey" 
-H "Content-Type: application/json" 
-d '{ "message": { "notification": { 
                       "title": "FCM Message", 
                        "body": "This is an FCM Message", },
      "to": "MyDeviceToken"}
}' https://fcm.googleapis.com/fcm/send 

This is the response I got

{"multicast_id":8835070472349844293,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1555418431449800%06445bacf9fd7ecd"}]}

My app was not running in the foreground when this request was made. So the notification should be delivered and displayed by google services but this is not happening.

Here is my manifest file code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.srishti.elderly">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SignUp"
            android:label="SignUp" />
        <activity
            android:name=".SignIn"
            android:label="SignIn" />

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

        </service>

        <activity android:name=".Home"
            android:label="Home"></activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher_background"/>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent"/>

    </application>

</manifest>

And this is my onMessageReceived function

 public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.d(TAG, "on message received");

        final Context context = this;
        Intent intent = new Intent(this, MainActivity.class);
        final PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);


        Notification noti = new Notification.Builder(this)
                .setContentTitle("FTest")
                .setContentText("Firebabse notification test")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pIntent)
                .build();


        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);
    }

Update:

This is the new json I tried sending it with:

curl -X POST 
-H "Authorization:key=Authorizationkey" 
-H "Content-Type: application/json" 
-d '{ 
     "data": { "title": "FCM Message", "body": "This is an FCM Message" }, 
     "to": "devicetoken"
}' https://fcm.googleapis.com/fcm/send 

Update: There was a problem with my device when I ran it on a different one it worked. Thanks.

srishti77714
  • 85
  • 1
  • 10
  • 1
    which means there is something wrong with the implementation in the application, so we would need to see that code to figure out what went wrong. – Joachim Haglund Apr 16 '19 at 13:51
  • Should I post my manifest file code here ? Because the onMessageReceived method will not be called as it is a notification message, should I post that too ? – srishti77714 Apr 16 '19 at 13:52
  • 1
    If we are only talking about background notifications, then only really manifest is needed, but it would be good to know if it works when app is open aswell – Joachim Haglund Apr 16 '19 at 13:54
  • 1
    What is your device android version ? – ismail alaoui Apr 16 '19 at 13:58
  • @ismailalaoui it is Android 6.0 (API level 23) – srishti77714 Apr 16 '19 at 13:59
  • Are you getting debug inside this method? If No then you are doing something wrong with your configuration on Firebase Console. Also please confirm gradle dependencies you have added in your gradle files and added google-services.json inside app. – Ajay Mehta Apr 16 '19 at 14:04

1 Answers1

1

The problem is your are sending message which only works when application is on foreground , to make it works when the application is in background you should use data , try this form :

{
  "data": {
      "title":"FCM Message", 
      "Body":"This is an FCM Message"            
  }, 
 "to" : "devicetoken"
}  

Explanation:

  • notification - messages that goes directly to Android's Notification tray only if your application is in background/killed or gets delivered to onMessageReceived() method if your app is in foreground.

  • Data payload - Doesn't matter whether your application is in forground or background or killed, these messages will always be delivered to onMessageReceived() method.

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38