[英]Can I use Apple Push Notification Service to have my app do something without notifying user in iOS 7
My Algorithm wishes to work this way- 我的算法希望以此方式工作-
If some desired event occurs at my server then I wish to send a small amount of data to every user my app (foreground or background). 如果我的服务器发生了某些所需的事件,那么我希望向我的应用程序(前台或后台)的每个用户发送少量数据。 But, instead of notifying the user I wish my app to do some computation on data and acknowledge server.
但是,我希望我的应用程序不通知用户,而是对数据进行一些计算并确认服务器。 Remember user must not be notified.
请记住,不得通知用户。
您可以在iOS 7上执行此操作。请参阅iOS应用程序编程指南中的 “定期获取少量内容”部分。
You can use the content-available
key in the push notification data and application:didReceiveRemoteNotification:fetchCompletionHandler:
. 您可以在推送通知数据和
application:didReceiveRemoteNotification:fetchCompletionHandler:
使用content-available
键application:didReceiveRemoteNotification:fetchCompletionHandler:
. It could be considered a bit of a hack for your use case and if you try to use it too much you will get limited by Apples system. 对于您的用例来说,它可能算是一点技巧,如果您尝试使用过多,将会受到Apples系统的限制。
You can send "empty" push notification (no text, no sound, no icon). 您可以发送“空”推送通知(没有文字,没有声音,没有图标)。 This kind of notifications is allowed and can be used for synchronization with server (to avoid the expensive repetitive polling from the app side).
允许这种通知,并且可以用于与服务器同步(以避免从应用程序侧进行昂贵的重复轮询)。
If your app is not running then such a notification won't show up among springboard notifications. 如果您的应用未运行,则此类通知将不会显示在跳板通知中。
If it is running then it will receive notification - you just have to make sure you don't show the empty ones (if you are also using notifications with actual contents). 如果它正在运行,它将收到通知-您只需要确保不显示空的(如果您还使用具有实际内容的通知)即可。 Your iOS code would look a bit like:
您的iOS代码如下所示:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:@"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:@"alert"];
}
if (message)
{
if (![message isEqualToString:@""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: @"notification"
message: message
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
else
{
//this was an empty notification - here you can enqueue
//your server-synchronization routine - asynchronously if possible
}
}
If you don't intend to use any "real" notifications you don't even have to bother with decoding the message. 如果您不打算使用任何“真实”通知,您甚至不必费心解码消息。
If you use ApnsPHP your message generation code would look like this: 如果您使用ApnsPHP,您的消息生成代码将如下所示:
$message = new ApnsPHP_Message($device_apns_token);
$message->setText('');
$message->setSound('');
$message->setExpiry(3600);
$push->add($message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.