ios - 徽章数量始终为一个(云功能)
问题描述
如果节点在 firebase 中成功更改,我已从云功能发送通知,但即使我发送多个通知,徽章编号也始终显示 (1)。
我在云函数中的代码(打字稿):
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()
}
}
我的代码可以正常工作并完成所有工作,但它的徽章编号保留在一个任何帮助中,这将非常感谢。
解决方案
在您的云功能(打字稿)中,您在徽章中发送 1,因此只会得到 1,将徽章编号更改为 2 或 3,然后它将反映在 didReceive 方法中。
您应该放置一些逻辑以使徽章编号在云功能(打字稿)中动态化。
推荐阅读
- python - Plotly 在图例框中显示不完整的格式化跟踪名称
- firefox-addon - Firefox WebExtension - 使用浏览器凭据获取页面 HTML
- android - 为什么我的应用没有按应有的方式连接到 Firestore 数据库?
- php - WooCommerce 按自定义字段更改结帐时的总价
- whmcs - 如何在 WHMCS 自定义页面中包含文章?
- javascript - 在 FabricJS 中获取选定对象的边界框的大小
- python - 如果我必须在每周五上午 10 点运行 cron 作业,这是仪式设置:0 17 5 * * *
- python - 为什么 python 找不到 cwiid 模块?
- mosel - Xpress mosel 中的 abs 和 mpvar^integer 错误
- survey - 我应该使用什么高级文本标签将图像从 Word 文档导入到 Qualtrics?