• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - 使用 cordova 在 Android/Ios 中显示角标(Badge)编号

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:52:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 cordova 开发一个混合应用程序。我想在收到来自 APNS/FCM 的通知后,在应用程序图标中显示通知计数,如下图所示。我正在使用 cordova-plugin-fcm 和 cordova-plugin-badge 插件。

代码:

onDeviceReady: function() {     
cordova.plugins.notification.badge.hasPermission(function (granted) {
    });

    cordova.plugins.notification.badge.registerPermission(function (hasPermission) {
    });

    // switch on 'auto clear'
    cordova.plugins.notification.badge.configure({
    autoClear: true
    }); 
//FCMPlugin.getToken( successCallback(token), errorCallback(err) ); 
    //Keep in mind the function will return null if the token has not been established yet. 
    FCMPlugin.getToken(
    function(token){            
        console.log("token "+token);
    },
    function(err){
        console.log('error retrieving token: ' + err);
    }
    )

    //FCMPlugin.subscribeToTopic( topic, successCallback(msg), errorCallback(err) ); 
    //All devices are subscribed automatically to 'all' and 'ios' or 'android' topic respectively. 
    //Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}". 
    FCMPlugin.subscribeToTopic('all');

    //FCMPlugin.onNotification( onNotificationCallback(data), successCallback(msg), errorCallback(err) )
    //Here you define your application behaviour based on the notification data.

    FCMPlugin.onNotification(
    function(data){     
        //increase the badge        
        cordova.plugins.notification.badge.increase();
        if(data.wasTapped){
        //Notification was received on device tray and tapped by the user.              

        }else{
        //Notification was received in foreground. Maybe the user needs to be notified.             
        }
    },
    function(msg){
    console.log('onNotification callback successfully registered: ' + msg);         
    },
    function(err){
        console.log('Error registering onNotification callback: ' + err);
    }
    );}

Image with badge !!



Best Answer-推荐答案


解决方案是发送 2 条不同的 FCM 消息。

第一个没有通知,负载如下:

{
  "data":{
    "badge":"true",
  },
    "to":"/topics/all",
    "priority":"high"
}

这将触发 MyFirebaseMessagingService.java 中的 onMessageReceived,然后将负载推送到 FCMPlugin.onNotification() 回调。

FCMPlugin.onNotification(function(data){
    if(data.badge == "true"){
        cordova.plugins.notification.badge.increase();
    }
});

通过这样做,您将增加角标(Badge)数量。 然后,您使用这样的有效负载发送通知:

{
  "notification":{
    "title":"Notification title",
    "body":"Notification body",
    "sound":"default",
    "click_action":"FCM_PLUGIN_ACTIVITY",
    "icon":"fcm_push_icon"
  },
    "to":"/topics/all",
    "priority":"high"
}

这样您将在通知托盘中收到消息。我还建议设置 cordova.plugins.notification.badge.set(0) onDeviceReady 和 onResume 事件,以便在应用程序进入前台后清除角标(Badge)编号。

关于android - 使用 cordova 在 Android/Ios 中显示角标(Badge)编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40112391/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap