我知道对此主题有很多疑问,但我的问题略有不同。
我正在使用 Firebase 云消息传递来通知客户端某些操作。当应用程序被终止或处于后台运行时,并且据我所知,在调试时,我可以看到,当设备接收到通知时,未触发 onMessageReceived 方法,则该类
方法{strong> onMessageReceived 所在的FCMNotificationIntentService extends FirebaseMessagingService {...}
以及默认的构造函数,例如:
public class FCMNotificationIntentService extends FirebaseMessagingService {
public FCMNotificationIntentService() {
super();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage);
}
//other methods and stuff...
}
因此,当设备接收到通知时,它不会触发 onMessageReceived ,而只会触发构造函数,这意味着通知将在没有数据的情况下到达设备。为什么会发生这种情况,当应用程序处于前台时,总是会触发 onMessageReceived ,并且有服务器提供的数据。
任何建议或想法都受到高度赞赏。
答案 0 :(得分:0)
您是否覆盖onNewToken,并且是否将令牌发送到服务器以允许与设备进行通信?在您的服务类别中添加:
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
您可以参考此https://github.com/firebase/quickstart-android/issues/548
答案 1 :(得分:0)
尝试使用它的工作代码
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;
}
//
// }
}