![](/img/trans.png)
[英]Is it possible to use server-side events to notify an iOS user despite my app not running, without using Apple Push Notification Service?
[英]Can I use Apple Push Notification Service to have my app do something without notifying user in iOS 7
我的算法希望以此方式工作-
如果我的服务器发生了某些所需的事件,那么我希望向我的应用程序(前台或后台)的每个用户发送少量数据。 但是,我希望我的应用程序不通知用户,而是对数据进行一些计算并确认服务器。 请记住,不得通知用户。
您可以在iOS 7上执行此操作。请参阅iOS应用程序编程指南中的 “定期获取少量内容”部分。
您可以在推送通知数据和application:didReceiveRemoteNotification:fetchCompletionHandler:
使用content-available
键application:didReceiveRemoteNotification:fetchCompletionHandler:
. 对于您的用例来说,它可能算是一点技巧,如果您尝试使用过多,将会受到Apples系统的限制。
您可以发送“空”推送通知(没有文字,没有声音,没有图标)。 允许这种通知,并且可以用于与服务器同步(以避免从应用程序侧进行昂贵的重复轮询)。
如果您的应用未运行,则此类通知将不会显示在跳板通知中。
如果它正在运行,它将收到通知-您只需要确保不显示空的(如果您还使用具有实际内容的通知)即可。 您的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
}
}
如果您不打算使用任何“真实”通知,您甚至不必费心解码消息。
如果您使用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.