gpt4 book ai didi

ios - 角标(Badge)数量始终为1(云函数)

转载 作者:行者123 更新时间:2023-11-30 11:10:18 25 4
gpt4 key购买 nike

如果节点在 firebase 中成功更改,我已从云功能发送通知,但角标(Badge)编号始终显示 (1),即使我发送多个通知。

我在云函数中的代码( typescript ):

import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
admin.initializeApp();
exports.sendPushNotificationToUpdate = functions.database.ref('/orders/{Id}/OrderStatus').onWrite((snapshot, context) => {
let status = snapshot.after.val();
console.log('status : ' + status);
if (status != 'Done' && status != 'Rejected') {
return null;
}

else {
const Id = context.params.Id;

console.log('id : ' + Id);

return admin.database().ref('/orders/' + Id).once('value').then(function (snap) {
const tokenId = snap.val().tokenId;
console.log('tokenId : ' + snap.val().tokenId);
let payload = {
notification: {
title: 'my App',
body: '',
badge: '1',
sound: 'default',
}
}
if (status == 'Done') {
payload = {
notification: {
title: 'My App',
body: 'your order is done',
badge: '1',
sound: 'default',
}
}


}
else if (status == 'Rejected') {
payload = {
notification: {
title: 'My App',
body: 'your order is rejected',
badge: '1',
sound: 'default',
}
}

}
return admin.messaging().sendToDevice(tokenId, payload).then(response => {
console.log('update respose');
console.log(response);
});
});
}


});

我在 Xcode 中的代码(swift):

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("tap on on forground app",userInfo)
Messaging.messaging().appDidReceiveMessage(userInfo)
let content = response.notification.request.content
let badgeNumber = content.badge as! Int
UIApplication.shared.applicationIconBadgeNumber = badgeNumber + 1

completionHandler()
let actionIdentifier = response.actionIdentifier
switch actionIdentifier {
case UNNotificationDismissActionIdentifier: // Notification was dismissed by user

completionHandler()
case UNNotificationDefaultActionIdentifier: // App was opened from notification

completionHandler()
default:
completionHandler()
}
}

我的代码工作正常,可以完成所有工作,但角标(Badge)编号保留在其中,如果有任何帮助,那就太好了,非常感谢。

最佳答案

在您的云函数( typescript )中,您在角标(Badge)中发送 1,因此只会收到 1,将角标(Badge)编号更改为 2 或 3,然后它将反射(reflect)在 didReceive 方法中。

您应该添加一些逻辑,使角标(Badge)编号在云函数( typescript )中动态化。

关于ios - 角标(Badge)数量始终为1(云函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52254562/

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