微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cordova-Plugin-Firebase当应用从OFF变为ACTIVE时,onNotificationOpen()在Android上不起作用

我在 Android设备上的Ionic2项目中实现了Cordova-Plugin-Firebase.

我按照指南进行操作.关于函数onNotificationopen(),如doc of the plugin中所述:

App is in background:

  • User receives the notification message in its device notification bar

  • User taps the notification and the app opens

  • User receives the notification data in the JavaScript callback

如果应用程序已关闭(不在后台),如果我发送推送通知,它会:

  • User receives the notification message in its device notification bar

然后,我点击面板中显示所有通知通知,应用程序加载并打开.

如果应用程序处于后台,则以下功能会在应用程序占据屏幕前景时触发某些事件:

Firebase.onNotificationopen().subscribe(
       (res) => { console.info(res)},(err) => { console.info(err)},() => {console.log("completed)}
);

但是当应用程序关闭时,似乎上面的这个功能没有设置为在应用程序开启时运行.因此,通过点击通知面板中显示通知,不会发现应用程序已打开.

这是正常的:如果是,是否有文件解释为什么在某处;是否还有另一种方法可以通过事件检测到已经点击通知消息以将应用程序从OFF打开到ACTIVE?如果没有,人们会知道我的错误是什么吗?

解决方法

必须在您自己的代码中的某处处理推送通知(如深层链接) – 在某个地方您需要确定通知打开的活动以及在这种情况下会发生什么.因此,您无法将其推迟到Firebase来为您检测.

当应用程序停止运行时,单击推送通知会将带有收到数据的Intent发送到您的应用程序.这是你可以处理它的意图(例如在onStart / Resume()中).如果您在应用程序从通知启动时记录可用的意图,您将看到它包含类似以下内容内容

Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.my.package cmp=com.my.package/.MyMainActivity (has extras) }
Extras { 
   google.sent_time > 1490275998196,from > 9994599943,google.message_id > 0:14902799999965%346177c211bbbb7c2,collapse_key > com.my.package 
}

这意味着你可以做类似的事情

if (getIntent() != null && getIntent().getExtras() != null) {
    //identify whether this was a push notification intent
    if (getIntent().getExtras().get("google.message_id") != null) { 
        //Here you can do the same things you do when opened from background
        console.log("completed"");
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐