我正在尝试让我的远程通知正常工作,但我遇到了一些问题。
我正在从我的服务器发送通知,并将 content-available
标志设置为 1
,这样我的 didReceiveRemoteNotification
就会被触发,我通过在 didReceiveRemoteNotification
中触发以下方法向用户显示通知:
- (void) showPush:(NSString *)message
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = message;
content.sound = [UNNotificationSound defaultSound];
NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
}];
}
应用处于挂起状态时发送的通知根本不显示,而是在应用打开并再次关闭后显示,如下所示:
-> App is suspended: notification is sent
-> App is opened and in the foreground: I can see that the notification has been processed
-> App is closed and in the background: notification is being displayed with a banner
对于为什么这不能按预期工作的任何建议,我们将不胜感激。
最佳答案
我认为您需要从一个略有不同的应用委托(delegate)回调中调用 showPush
:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
此回调说明的文档:
Unlike the application(_:didReceiveRemoteNotification:) method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background.
关于ios - 远程通知未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53178391/