Reputation: 491
I know there are a bunch of question about this topic but my problem is a little bit different.
I'm using Firebase cloud messaging to notify the clients for a certain action. When the app is killed or is in background and as far as I'm on the debug, I can see that when the notification is received by device the onMessageReceived method is not being triggered, I have the class
FCMNotificationIntentService extends FirebaseMessagingService {...}
on which the method onMessageReceived is located and just a default constructor like:
public class FCMNotificationIntentService extends FirebaseMessagingService {
public FCMNotificationIntentService() {
super();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage);
}
//other methods and stuff...
}
so when the notification is received by the device it is not triggering onMessageReceived but only the constructor, that means the notification is coming to device without data. Why is this happening, when the app is in foreground the onMessageReceived is always being triggered and there are the data given from server.
Any suggestion or idea is highly appreciated.
Upvotes: 0
Views: 873
Reputation: 1802
Try this its working code
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Data: " + remoteMessage.getData());
// Log.d(TAG, "Notification Message Body: " +
remoteMessage.getNotification().getBody());
String message_title = "";
String from_user_id = "";
String to_user_id = "";
String type = "";
String sound = "";
String vibrate = "";
String message = "";
try {
message_title = remoteMessage.getData().get("message_title");
from_user_id = remoteMessage.getData().get("from_user_id");
to_user_id = remoteMessage.getData().get("to_user_id");
type = remoteMessage.getData().get("type");
sound = remoteMessage.getData().get("sound");
vibrate = remoteMessage.getData().get("vibrate");
message = remoteMessage.getData().get("message");
} catch (Exception e) {
e.printStackTrace();
}
//Calling method to generate notification
sendNotification(message_title,from_user_id,to_user_id,type,sound,vibrate,message);
}
boolean whiteIcon = (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.LOLLIPOP);
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String message_title, String from_user_id,String
to_user_id, String type, String sound,String vibration,String message) {
// Intent intent = new Intent(this, MainActivity.class);
// if (AppData.getInstance().getReader(this)==null){
Intent intent = new Intent(this, ChatListActivity.class);
// }else {
// intent = new Intent(this, ArticlesActivity.class);
//
intent.putExtra(Const.getInstance().iFrom,Const.getInstance().notification);
// intent.putExtra("notification",1);
// }
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri=
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new
NotificationCompat.Builder(this)
// .setSmallIcon(R.mipmap.ic_launcher)
.setSmallIcon(getNotificationIcon())
.setContentTitle(message_title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setColor(whiteIcon ? getResources().getColor(R.color.colorPrimary) :
getResources().getColor(android.R.color.transparent));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
private int getNotificationIcon() {
boolean whiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
return whiteIcon ? R.drawable.notification: R.mipmap.ic_launcher;
}
//
// }
}
Upvotes: 0
Reputation: 359
Did you override onNewToken and do you send a Token to your server to allow communication to the device? In your service class add:
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
You can refer to this https://github.com/firebase/quickstart-android/issues/548
Upvotes: 0