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

时间:2015-08-13 03:30:06

标签: azure azure-mobile-services phonegap-pushplugin ngcordova

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

我在iOS应用程序和ngCordova Push插件中使用Ionic / Phonegap。我想为iOS设备添加徽章数量,但我似乎无法找到实现此目的的方法。我尝试过使用push.apns.send函数,但无法使用它。

Azure移动服务

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 个答案:

答案 0 :(得分:2)

我终于明白了。问题是模板注册需要包含徽章。这有什么作用:

Azure移动服务

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)
});