我正在构建一个本机应用程序。每当我们的云发送APNS消息时,我们都希望将徽章增加1。
我使用react-native PushNotificationIOS
模块来处理消息,但是a known limitation使用react-native,当应用程序处于启用状态时,JS桥将被停用的背景。因此,想要操作徽章的反应原生应用程序代码不会被执行,因为通知事件永远不会传递给javascript。
即使app不在前台,这个徽章操作也很重要,所以我决定直接在AppDelegate.m中实现Objective-C中的徽章递增逻辑。 (注意:我不了解ObjC,只能松散地遵循它。)
这是来自AppDelegate.m的全部didReceiveRemoteNotification
:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
// --- BEGIN CODE I ADDED --- //
// increment badge number; must be written in ObjC b/c react-native JS bridge is deactivated when app in background
NSLog(@"remote notification LINE 1");
NSUInteger currentBadgeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
NSLog(@"currentBadgeNum = %d", currentBadgeNum);
NSUInteger newBadgeNum = currentBadgeNum + 1; // to increment by .badge: ... + [[[notification objectForKey:@"aps"] objectForKey: @"badge"] intValue]
NSLog(@"newBadgeNum = %d", newBadgeNum);
[UIApplication sharedApplication].applicationIconBadgeNumber = newBadgeNum;
NSLog(@"done updating badge; now notifying react-native");
// --- END CODE I ADDED --- //
// forward notification on to react-native code
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
}
当我测试它时,当应用程序背景化时,Xcode中没有任何日志语句出现。但是,当应用程序处于活动状态时,会显示日志语句,并且例程与预期完全一致(并且通知也会传递给javascript代码)。
我相信我已经将应用配置为接收推送通知;这是我的Info.plist中的相关部分:
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
我错过了什么?我的代码有问题,还是我误解了didReceiveRemoteNotification
的性质?
感谢任何帮助。