Firebase 通知,应用在通知点击后无法打开



我知道有很多关于这个话题的问题,但我的问题有点不同。

我正在使用Firebase云消息传递来通知客户执行特定操作。当应用程序被杀死或在后台时,就我在调试中而言,我可以看到当设备收到通知时,onMessageReceived方法未被触发,我有类 方法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,并且有来自服务器的数据。

任何建议或想法都受到高度赞赏。

您是否覆盖了 onNewToken,并且是否向服务器发送了令牌以允许与设备通信?在服务类中添加:

@Override
public void onNewToken(String s) {
super.onNewToken(s);
}

您可以参考此 https://github.com/firebase/quickstart-android/issues/548

试试它的工作代码

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;
}
// 
//    }
}

相关内容

  • 没有找到相关文章

最新更新