首页 文章

Cordova / Phonegap上带有iOS徽章的Azure推送通知

提问于
浏览
1

我有以下代码可以使用Azure通知中心发送推送通知 . 将新项目插入数据库时,此代码会向使用该标记注册的设备发送推送通知 .

我正在为iOS应用程序和ngCordova Push插件使用Ionic / Phonegap . 我想为iOS设备添加徽章计数,但我似乎无法找到这样做的方法 . 我已经尝试过使用push.apns.send函数,但无法让它工作 .

Azure Mobile Services

function insert(item, user, request) {
    // Execute the request and send notifications.
    request.execute({
       success: function() { 
            // Create a template-based payload.
            var payload = '{ "message" : "This is my message" }';            

            push.send("My Tag", payload, {          
               success: function(pushResponse){ 
                   // Send the default response.
                   request.respond();
               },              
               error: function (pushResponse) {
                   console.log("Error Sending push:", pushResponse);
                    // Send the an error response.
                   request.respond(500, { error: pushResponse });
                   }           
            });                
       }
   });   
}

Phonegap

var iosConfig = {
    "badge": true,
    "sound": true,
    "alert": true
};

$cordovaPush.register(iosConfig).then(function (deviceToken) {
    var hub = new NotificationHub(mobileClient);

    // This is a template registration.
    var template = "{\"aps\":{\"alert\":\"$(message)\"}}";

    // Register for notifications.
    // (deviceId, ["tag1","tag2"], templateName, templateBody, expiration)
    hub.apns.register(deviceToken, myTags, "myTemplate", template, null).done(function () {
        // Registered with hub!
    }).fail(function (error) {
        alert("Failed registering with hub: " + error);
    });

}, function (err) {
    alert("Registration error: " + err)
});

我搜索了几十篇文章/教程,但都没有 . 任何帮助将不胜感激 .

1 回答

  • 2

    我终于弄明白了 . 问题是模板注册需要包含徽章 . 这是有效的:

    Azure Mobile Services

    function insert(item, user, request) {
        // Execute the request and send notifications.
        request.execute({
           success: function() { 
                // Create a template-based payload.
                var payload = '{ "message" : "' + originalMessage + '", "badge" : "100" }';            
    
                push.send("My Tag", payload, {          
                   success: function(pushResponse){ 
                       // Send the default response.
                       request.respond();
                   },              
                   error: function (pushResponse) {
                       console.log("Error Sending push:", pushResponse);
                        // Send the an error response.
                       request.respond(500, { error: pushResponse });
                       }           
                });                
           }
       });   
    }
    

    Phonegap

    var iosConfig = {
        "badge": true,
        "sound": true,
        "alert": true
    };
    
    $cordovaPush.register(iosConfig).then(function (deviceToken) {
        var hub = new NotificationHub(mobileClient);
    
        // This is a template registration.
        var template = "{\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\" }}";
    
        // Register for notifications.
        // (deviceId, ["tag1","tag2"], templateName, templateBody, expiration)
        hub.apns.register(deviceToken, myTags, "myTemplate", template, null).done(function () {
            // Registered with hub!
        }).fail(function (error) {
            alert("Failed registering with hub: " + error);
        });
    
    }, function (err) {
        alert("Registration error: " + err)
    });
    

相关问题