![](/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.