如果节点成功在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中的代码(快速):
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()
}
}
我的代码可以很好地工作并完成所有工作,但徽章编号始终对任何帮助都将有很大帮助。
答案 0 :(得分:0)
在云功能(打字稿)中,您将在徽章中发送1,因此只会得到1,将徽章编号更改为2或3,然后它将反映在didReceive方法中。
您应该提出一些逻辑,以使云功能(打字稿)中的徽章编号动态化。
答案 1 :(得分:0)
更改此行:
UIApplication.shared.applicationIconBadgeNumber += 1