gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:52:59 25 4
gpt4 key购买 nike

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

我正在将 Ionic/Phonegap 用于 iOS 应用程序和 ngCordova 推送插件。我想为 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 });
}
});
}
});
}

电话间隙

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

我搜索了数十篇文章/教程,但没有一个起作用。任何帮助将不胜感激。

最佳答案

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

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

电话间隙

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

关于Cordova/Phonegap 上带有 iOS 徽章的 Azure 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com