在Android 8中未收到Cloud Function FCM消息 [英] Not receiving Cloud function FCM message in android 8

查看:77
本文介绍了在Android 8中未收到Cloud Function FCM消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有云"下方的功能来发送FCM,在Android 7中已成功接收,但在Android 8中无法正常工作.

I have below Cloud function to send FCM, it is received successfully in Android 7, but I am not able to get it working in 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上未收到此消息! 因此,如文档所示,我更新了如下所示的优先级:

This is perfectly in Android 7 and below, but this message is not received on Android 8! So I updated priority like below, as documentation suggests now :

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

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

But adding this android -> priority here is giving me error in cloud function like below :

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

Error sending notification of start session: { Error: Messaging payload contains an invalid "android" property. Valid properties are "data" and "notification". at FirebaseMessagingError.Error (native)

这是怎么回事!

更新1:

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

Thanks AL, I have replaced sendToDevice() to send(), and also sending token also along with message object Result : I get this error in cloud function :

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

Error sending notification of start session: { Error: Firebase Cloud Messaging API has not been used in project my_project_id before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=my_project_id then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

我很快会再次更新.

更新2

我为我的项目启用了此API,并且等待了几分钟后,它开始在Android 7中显示通知.但是在Android 8上,我没有收到FCM消息(我在onMessageReceived上设置了断点,但它从未被命中) .以下是我的服务代码:

I enabled this API for my project and after waiting for few mins, it has started showing notification in Android 7. But on Android 8, I do not receive FCM message (I have put breakpoint on onMessageReceived but it never gets hit). Below is my service code :

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());
    .....
    }

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

Anyone having a clue about what can go wrong?

推荐答案

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

Ok this is how I got it working :

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

Firebase release notes say : FirebaseInstanceId service was deprecated, around 28 June 2018, and my tokens were update in subclass of that service. https://firebase.google.com/support/release-notes/android

在数据库中查找时,我注意到我的Android 7手机的Firebase令牌是相同的,但Android 8手机的Firebase令牌已更改,但是我的云功能无法获取最新的令牌,因为应用无法发送回更新的令牌.

While looking in my database I noticed that my firebase token for Android 7 phone was same, but firebase token for Android 8 phone had changed, but my cloud functions could not pickup latest token as app could not send back updated token.

所以我实现了FirebaseMessagingServiceonNewToken方法,现在我的令牌已更新,并且在Android O上收到了FCM消息.

So I implemented onNewToken method of FirebaseMessagingService and now my token is updated, and I get FCM messages on Android O.

这篇关于在Android 8中未收到Cloud Function FCM消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆