在Android 8中未收到Cloud function FCM消息

时间:2018-07-28 12:08:50

标签: android firebase firebase-cloud-messaging google-cloud-functions

我具有以下Cloud功能来发送FCM,它在Android 7中已成功接收,但是在Android 8中却无法正常工作。

我的云函数库版本为:

"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2"
var message = { 
                data : {  event: `${constants.NOTIF_EVENT_START_SESSION}` , payload : `${jsonData}`}
            }        
            var options = {
                priority: "high",
                timeToLive: 60 * 60 * 24
                };
            console.info(`Getting notification token for the user`)  

                const token = 'token_here'
                console.info(`Received notification token for the user ${token}`) 
                return admin.messaging().sendToDevice(token, message,options)
                .then((response) => {
                    console.info("Successfully sent notification of start session")
                }).catch(function(error) {
                    console.warn("Error sending notification of start session: " , error)
                })

这在Android 7及更低版本中非常理想,但在Android 8上未收到此消息! 因此,正如文档建议的那样,我如下更新了优先级:

    var message = { 
        data : {  event: `${constants.NOTIF_EVENT_START_SESSION}` , payload : `${jsonData}`}
        , android : { priority : "high" }
    } 

但是在此处添加此android->优先级会给我以下云函数的错误:

  

发送开始会话的通知时出错:{错误:消息传递   有效负载包含无效的“ android”属性。有效属性为   “数据”和“通知”。       在FirebaseMessagingError.Error(本机)上

这是怎么回事!

更新1:

感谢AL,我将sendToDevice()替换为send(),并且还与消息对象一起发送了令牌 结果: 我在云端功能中收到此错误:

  

发送启动会话通知时出错:{错误:Firebase Cloud   之前尚未在项目my_project_id中使用消息传递API   被禁用。通过访问启用它   https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=my_project_id   然后重试。如果您最近启用了此API,请等待几分钟   传播到我们的系统并重试的操作。

我很快会再次更新。

更新2

我为我的项目启用了此API,并且等待了几分钟后,它开始在Android 7中显示通知。但是在Android 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());
    .....
    }

任何人都知道可能出什么问题吗?

1 个答案:

答案 0 :(得分:0)

好,这就是我的工作方式:

Firebase发行说明说:FirebaseInstanceId服务已于2018年6月28日左右弃用,我的令牌已在该服务的子类中更新。 https://firebase.google.com/support/release-notes/android

在查看数据库时,我注意到我的Android 7手机的firebase令牌是相同的,但是Android 8手机的firebase令牌已更改,但是我的云功能无法获取最新的令牌,因为应用程序无法发送回更新的令牌。< / p>

因此我实现了onNewToken的{​​{1}}方法,现在我的令牌已更新,并且在Android O上收到了FCM消息。