Reputation: 187
When I don't send the notification object in the JSON request to the Firebase API, the onMessageReceive is triggered normally and my notifications work as intended, but, when I use the notification or the android object in the JSON that I send as params the onMessageReceive is not triggered when the app is in background.
I've been trying to debug this but I can't figure it out.
I have a simple class in my server that user Firebase API to send the push notifications.
In the params that I send to the server, I'm sending the following attributes.
"apns": {
"title": title.nil? ? "Testing notification" : title,
"body": body.nil? ? "This is a test push notification, liking it?" : body,
"mutable_content": true,
"sound": sound.nil? ? "enabled" : sound
},
"android": {
"title": title.nil? ? "Testing notification" : title,
"body": body.nil? ? "This is a test push notification, liking it?" : body,
"sound": sound.nil? ? "enabled" : sound
},
or the following for cross plataforms.
"notification": {
"title": title.nil? ? "Testing notification" : title,
"body": body.nil? ? "This is a test push notification, liking it?" : body,
"mutable_content": true,
"sound": sound.nil? ? "enabled" : sound
},
This configuration is suggested by google in the following official documentation
Firebase Documentation The only way it actually works is when I DO NOT SEND the notification or android object and generate the notification with the data I send in the data attribute of the JSON object sended to the Firebase API
params = {
"#{key}": to,
# "apns": {
# "title": title.nil? ? "Testing notification" : title,
# "body": body.nil? ? "This is a test push notification, liking it?" : body,
# "mutable_content": true,
# "sound": sound.nil? ? "enabled" : sound
# },
# "android": {
# "title": title.nil? ? "Testing notification" : title,
# "body": body.nil? ? "This is a test push notification, liking it?" : body,
# "sound": sound.nil? ? "enabled" : sound
# },
"data": {
"title": title,
"body": body
}
}.to_json
The notifications sent through the Firebase console don't work either.
¿Any ideas?
Upvotes: 1
Views: 581
Reputation: 1496
Use Below Xml Code in andorid manifest
<service
android:name=".services.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Create MyFirebaseMessaginfService Class to recive Firebase Notification When App is close.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
LocalBroadcastManager broadcaster;
@Override
public void onCreate() {
broadcaster = LocalBroadcastManager.getInstance(this);
}
@Override
public void onNewToken(@NonNull String token) {
Log.d(TAG, "Refreshed token: " + token);
Const.FireBaseTokenID=token;
Log.e(TAG, "onComplete:Service "+Const.FireBaseTokenID );
}
public MyFirebaseMessagingService() {
getFireBaseID();
}
public static void getFireBaseID() {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if(!task.isSuccessful()){
Log.w(TAG, "getInstance ID Failed "+task.getException() );
return;
}
String token = task.getResult().getToken();
Log.e(TAG, "onComplete:Service Method "+token );
}
});
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String call_api=remoteMessage.getData().get("callapi");
String play_id=remoteMessage.getData().get("play_id");
String clue_number=remoteMessage.getData().get("clue_number");
String title=remoteMessage.getData().toString();
Log.e(TAG, "onMessageReceived: callapi "+call_api );
Log.e(TAG, "onMessageReceived: play_id "+play_id );
Log.e(TAG, "onMessageReceived: clue number "+clue_number );
Log.e(TAG, "onMessageReceived: title "+title );
Intent intent= new Intent("remoteMessage");
intent.putExtra("call_api", call_api);
intent.putExtra("play_id", play_id);
intent.putExtra("clue_number", clue_number);
intent.putExtra("title", title);
broadcaster.sendBroadcast(intent);
}
}
Upvotes: 1
Reputation: 1018
You should send with format data, same:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
Upvotes: 0