2016-10-18 70 views
2

コードナビでハイブリッドアプリケーションを開発しています。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); 
    } 
    );} 

Image with badge !!

答えて

0

溶液は、2つの異なるFCMメッセージを送信することです。このようなペイロードを持つ通知なし

最初のものは、:

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

これは次いでFCMPlugin.onNotificationにペイロード()コールバックを押す、MyFirebaseMessagingService.javaにonMessageReceivedをトリガします。

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

これを行うと、バッジ番号が増えます。あなたは、通知トレイにメッセージが表示されます

{ 
    "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イベントを設定して、アプリがフォアグラウンドになった後にバッジ番号がクリアされるようにすることもおすすめします。

関連する問題