ios - Remote notifications not working as expected
问题描述
I'm trying to get my remote notifications to work properly, but I've encountered some problems.
I'm sending notifications from my server with the content-available
flag set to 1
, such that my didReceiveRemoteNotification
is being triggered, and I display the notification to the user by triggering the following method inside 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);
}
}];
}
Notifications being sent while the app is in the suspended state are not displayed at all, but are displayed after the app has been opened and then closed again, like this:
-> 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
Any suggestions to why this does not work as intended would be appreciated.
解决方案
我认为您需要showPush
从稍微不同的应用程序委托回调中调用:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
此回调说明的文档:
与 application(_:didReceiveRemoteNotification:) 方法不同,该方法仅在您的应用程序在前台运行时调用,系统在您的应用程序在前台或后台运行时调用此方法。
推荐阅读
- javascript - 在 selenium 中进行仿真,节点不工作
- spring - Hibernate diffChangeLog 生成表中已经存在的更改集
- javascript - 即使在控制台上得到响应也无法从 json 获取数据
- java - 如何在另一个活动的 Countdown OnFinish 时调用方法?
- kubernetes - 如何嗅探从 localhost 到我的 k8s 集群的流量(在本地运行)?
- javascript - 拒绝连接到 'ws://localhost:8080/ws',因为它违反了以下内容安全策略指令:“connect-src 'self'
- javascript - 将数学转换为 JavaScript;收敛/发散级数
- python - “用户”对象没有属性“mutual_guilds”
- paperjs - 如何在 PaperJS 中重复该动画?
- amazon-web-services - jq:错误:语法错误,意外的 INVALID_CHARACTER,在 Windows 中期望 $end