コードナビでハイブリッドアプリケーションを開発しています。APNS/FCMから通知を受け取った後、次の画像と同じアプリケーションアイコンに通知カウントを表示します。私はcordova-plugin-fcmとcordova-plugin-badgeプラグインを使用しています。cordovaを使用してAndroid/Iosのバッジ番号を表示
コード:
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);
}
);}