Reputation: 85
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.
Upvotes: 0
Views: 3844
Reputation: 6063
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.
Upvotes: 1