我有下面的云功能来发送FCM,它在Android 7中成功接收,但我无法在Android 8中工作。
我的云函数库版本是:
在安卓7及以下版本中是完美的,但在安卓8上没有收到此消息!所以我更新了如下优先级,正如文档所示:"firebase-admin": "^5.13.1", "firebase-functions": "^2.0.2"
var message = { data : { event: `${constants.NOTIF_EVENT_START_SESSION}` , payload : `${jsonData}`} , android : { priority : "high" } }
但在这里添加这个android->优先级会给我在云功能中的错误,如下所示:
发送启动会话通知时出错:{错误:消息有效载荷包含无效的"android"属性。有效属性为"数据"one_answers"通知"。在FirebaseMessagingError。错误(本机(
这里发生了什么!
更新1:
感谢AL,我已经将sendToDevice((替换为send((,并且还发送了令牌和消息对象结果:我在云功能中得到这个错误:
发送启动会话通知时出错:{错误:Firebase Cloud消息API以前未在项目my_project_id中使用过或它已禁用。通过访问启用https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=my_project_id然后重试。如果您最近启用了此API,请等待几分钟传播到我们的系统并重试的操作。
我很快会再次更新。
更新2
我为我的项目启用了这个API,等待了几分钟后,它开始在Android7中显示通知。但在安卓8上,我不会收到FCM消息(我在onMessageReceived上设置了断点,但它永远不会被击中(。以下是我的服务代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyMessagingService"; @Inject NotificationConstants constants; @Override public void onCreate() { super.onCreate(); ((MyApplication) getApplication()).getAppComponent().inject(this); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { setupNotificationChannels(); } } @TargetApi(Build.VERSION_CODES.O) private void setupNotificationChannels() { setupNotificationChannel(constants.SHARE_ID, constants.getShareName()); setupNotificationChannel(constants.REQUEST_ID, constants.getRequestName()); setupNotificationChannel(constants.OTHER_ID, constants.getOtherName()); } @TargetApi(Build.VERSION_CODES.O) private void setupNotificationChannel(String id, String name) { NotificationManager mgr = ((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))); NotificationChannel notificationChannel = new NotificationChannel(id, name, mgr.IMPORTANCE_HIGH); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.GREEN); notificationChannel.setShowBadge(true); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); mgr.createNotificationChannel(notificationChannel); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); ..... }
有人知道会出什么问题吗?
好吧,这就是我让它工作的方式:
Firebase发布说明称:FirebaseInstanceId
服务在2018年6月28日左右被弃用,我的令牌在该服务的子类中更新。https://firebase.google.com/support/release-notes/android
在查看我的数据库时,我注意到我的Android 7手机的firebase令牌是相同的,但Android 8手机的firebasetoken已经更改,但我的云功能无法获取最新的令牌,因为应用程序无法发回更新的令牌。
所以我实现了FirebaseMessagingService
的onNewToken
方法,现在我的令牌更新了,我在Android O上获得了FCM消息。