iOS: 在不显示通知的情况下推送到 运行 方法
iOS: Making push to run a method without displaying a notification
是否可以在 iOS 中从服务器获取推送,然后在 运行 中完全不显示通知的方法?例如,检查现在是否是配置的时间,是否显示通知,如果不是则向服务器发送不可用消息。
是的,您要找的是 silent push notification
得到 introduced in iOS 7. Take a look at the docs regarding push notifications 哪个州:
The aps dictionary can also contain the content-available
property. The content-available
property with a value of 1
lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
和
content-available
-
number
-
Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed, application:didReceiveRemoteNotification:fetchCompletionHandler:
is called.
(Newsstand apps are guaranteed to be able to receive at least one push with this key per 24-hour window.)
如果您觉得用户需要了解某事(虽然不是 100% 确定),您可以做出相应的反应并最终触发另一个通知。
可以找到更深入的信息和代码in this objc.io post。
是的,有可能:
确保注册远程通知,参见documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
还要确保编辑 Info.plist
并选中 "Enable Background Modes" 和 "Remote notifications" 复选框:
此外,您需要将 "content-available":1
添加到您的推送通知负载中,否则应用程序在后台时不会被唤醒(参见 documentation here):
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app
所以负载至少应该是这样的:
{
"aps" = {
"content-available" : 1,
"sound" : ""
}
}
只需将声音 属性 留空并省略 alert/text 属性,您的通知将 静音。
不幸的是,应用程序不会被唤醒,如果它根本不是 运行(强制退出),请参阅 this answer。
是否可以在 iOS 中从服务器获取推送,然后在 运行 中完全不显示通知的方法?例如,检查现在是否是配置的时间,是否显示通知,如果不是则向服务器发送不可用消息。
是的,您要找的是 silent push notification
得到 introduced in iOS 7. Take a look at the docs regarding push notifications 哪个州:
The aps dictionary can also contain the
content-available
property. Thecontent-available
property with a value of1
lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
和
content-available
-number
- Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed,application:didReceiveRemoteNotification:fetchCompletionHandler:
is called. (Newsstand apps are guaranteed to be able to receive at least one push with this key per 24-hour window.)
如果您觉得用户需要了解某事(虽然不是 100% 确定),您可以做出相应的反应并最终触发另一个通知。
可以找到更深入的信息和代码in this objc.io post。
是的,有可能:
确保注册远程通知,参见documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; return YES; }
还要确保编辑
Info.plist
并选中 "Enable Background Modes" 和 "Remote notifications" 复选框:此外,您需要将
"content-available":1
添加到您的推送通知负载中,否则应用程序在后台时不会被唤醒(参见 documentation here):For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app
所以负载至少应该是这样的:
{ "aps" = { "content-available" : 1, "sound" : "" } }
只需将声音 属性 留空并省略 alert/text 属性,您的通知将 静音。
不幸的是,应用程序不会被唤醒,如果它根本不是 运行(强制退出),请参阅 this answer。