![](/img/trans.png)
[英]Push Notification is not received when the app is in background mode or it is force stopped iOS 10
[英]App not responding to received push notification while stopped or in background
我正在实施Google Cloud Messaging服务。 只要我的应用程序在前台,一切都可以正常运行。 但是,如果我将应用程序置于后台或完全停止运行,则不再收到推送通知。 我遍历了GCM指南 , Apple开发人员页面以及许多SO帖子。
在我的info.p-list文件中,我选中了“ Background fetch
和“ Remote notifications
。 我不知道我想念的是什么。
这是AppDelegate中触发消息处理的方法。 它主要来自Google。 他们对“只有在应用启动了GCM服务后才起作用”的评论表明,还有其他事情需要发生,但我只是不知道它是什么。 我什至没有生成NSLog输出(即该方法根本不触发)。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
NSLog(@"Notification received: %@", userInfo);
//EDIT: looks like this line needs to go!!!!
// This works only if the app started the GCM service
//[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
if (application.applicationState == UIApplicationStateActive) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
} else {
if([application currentUserNotificationSettings].types & UIUserNotificationTypeAlert) {
UILocalNotification * localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = message;
localNotification.alertTitle = title;
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
[application presentLocalNotificationNow:localNotification];
}
}
handler(UIBackgroundFetchResultNoData);
}
还有什么需要补充的吗? 我还需要做什么? 我受阻了。 谢谢。
编辑-部分成功
好的,我发现这一行:
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
负责阻止该应用在后台运行时收到的通知。 评论出来解决了这个问题(这让我有些紧张,因为我不知道那条线的意思!)。
但是,当应用程序完全停止时,我仍然无法收到这些通知。
编辑2试图实施Ali的建议,但现在我很困惑。 当我阅读GCM文档时,我一直依赖于格式化我的PHP脚本的一个名为Send Downstream Messages ,我认为这对于发送到Android或iOS是通用的(GCM服务器可以处理任何差异,也许?)。 除了我在停止应用程序时描述的问题外,它非常适合Android消息,也适用于iOS。
Ali使我认为iOS需要使用不同的HTTP格式,因为他指向文档“在iOS上设置客户端应用程序” ,我一直在认真地遵循我的Objective-C代码。 它描述的HTTP格式略有不同,但可能是至关重要的要求。 具体来说,URL更改为https://gcm-http.googleapis.com/gcm/send
并且您在邮件正文中添加了另一个键值对content_type:true
。
问题是,一旦执行此操作,就不会收到任何消息。 我真的需要进行这些更改吗? 也许我只是在错误地实施它们。 这是我的iOS的PHP脚本:
<?php
function send_to_ios_devices($link, $api_access_key, $club_id, $notification) {
$headers = array(
'Authorization: key=' . $api_access_key,
'Content-Type: application/json'
);
$registration_ids = array();
$sql = "SELECT gcm_registration_token
FROM USERS
WHERE device='ios' AND club_id=$club_id";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)){
$reg_id = $row['gcm_registration_token'];
if ($reg_id != -1) array_push($registration_ids, $reg_id);
}
$fields = array(
'registration_ids' => $registration_ids,
'content_available' => 'true', //<--ADDED THIS LINE
'notification' => $notification
);
$ch = curl_init();
//curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
$recipients = count($registration_ids);
echo "Push notification sent to " . $recipients . " ios recipient(s).<br>";
}
?>
您必须在节中添加"content_available" : "true"
才能在ios上唤醒应用程序。
在这里您可以找到完整的文档
在iOS上,如果应用程序在后台运行,则取决于APN是否传递通知。 如果您的下游消息需要用户采取措施,则可以使用优先级:“高”,即使应用程序在后台,它也会在iOS上发送通知。 请注意,在生产环境中,您仅应将“高优先级”用于需要用户交互的通知,例如聊天消息。 否则,请使用“正常”优先级。
检查参考以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.