首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android 8未收到云函数FCM消息

android 8未收到云函数FCM消息
EN

Stack Overflow用户
提问于 2018-07-28 20:08:50
回答 1查看 1.5K关注 0票数 1

我有下面的云函数来发送FCM,它在Android 7中成功接收,但我无法让它在Android 8中工作。

我的云函数库版本如下:

"firebase-admin":"^5.13.1","firebase-functions":"^2.0.2“

代码语言:javascript
复制
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上不会收到这条消息!因此,我更新了优先级,如下所示,正如文档所建议的那样:

代码语言:javascript
复制
    var message = { 
        data : {  event: `${constants.NOTIF_EVENT_START_SESSION}` , payload : `${jsonData}`}
        , android : { priority : "high" }
    } 

但是在这里添加这个android ->优先级会在云函数中给出错误,如下所示:

发送启动会话通知时出错:{错误:消息负载包含无效的"android“属性。有效的属性是“数据”和“通知”。在FirebaseMessagingError.Error (本机)

这是怎么回事!

更新1:

感谢AL,我已经将sendToDevice()替换为send(),并且还将token与message object Result一起发送:我在云函数中遇到以下错误:

发送启动会话通知时出错:{错误: Firebase Cloud Messaging

以前未在项目my_project_id中使用或已禁用。通过访问https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=my_project_id启用它,然后重试。如果您最近启用了此API,请等待几分钟,让操作传播到我们的系统,然后重试。

我很快就会再次更新。

更新2

我为我的项目启用了这个接口,等待了几分钟后,它开始在Android7中显示通知。但在Android8上,我没有收到FCM消息(我在onMessageReceived上设置了断点,但它从未命中)。以下是我的服务代码:

代码语言:javascript
复制
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());
    .....
    }

有谁知道哪里会出问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 04:31:29

好的,这就是我是如何让它工作的:

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

当我查看我的数据库时,我注意到我的Android 7手机的firebase令牌是相同的,但Android 8手机的firebase令牌已经改变了,但我的云函数无法获取最新的令牌,因为应用程序无法发回更新的令牌。

所以我实现了FirebaseMessagingServiceonNewToken方法,现在我的令牌更新了,我在Android O上得到了FCM消息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51571235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档