我试图在后台接收推送通知,但似乎不起作用。下面是我的AppDelegate代码:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
var notification = UILocalNotification()
notification.alertBody = "Nova poruka od korisnika "+(userInfo["gcm.notification.username"] as! String) // text that will be displayed in the notification
notification.fireDate=NSDate(timeIntervalSinceNow: 0)
notification.applicationIconBadgeNumber=0
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.userInfo=userInfo // assign a unique identifier to the notification so that we can retrieve it later
notification.timeZone=NSTimeZone.localTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(notification)
completionHandler(UIBackgroundFetchResult.NoData)
}当我在前台时,我收到了对此函数的回调,但只要我点击home按钮,就没有收到通知(我猜VPN部分失败了)。
下面是发送通知的PHP代码:
(我知道这是可怕的代码,但我正在调试atm :P)
foreach ($iosIDs as $id) {
$fields = array("to" => $id, "notification" => $msg, "priority" => "high", "content_available" => true);
$ch = curl_init();
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);
echo $result;
curl_close($ch);
}我已经读了至少10个这样的问题,但似乎想不出来。非常感谢。
发布于 2016-08-12 15:29:07
确保将"priority": "high"添加到JSON中。这样,您就可以在后台获得通知。
GCM尝试立即传递高优先级消息,允许
服务在可能的情况下唤醒休眠设备,并打开到应用服务器的网络连接。例如,具有即时消息、聊天或语音呼叫提醒的应用程序通常需要打开网络连接,并确保GCM立即将消息发送到设备。仅当消息是时间关键型消息并需要用户立即交互时才设置高优先级,并且要注意,与普通优先级消息相比,将消息设置为高优先级会导致更多的电池耗尽。
如果这还不起作用,我在这个thread中发现您应该以正确的格式发送数据。示例:
{
"notification":{
"badge":"12",
"alert":"default",
"sound":"default",
"title":"default"
},
"content_available":true,
"to":"YOUR_KEY_HERE"
}您也可以在此link上查看。
https://stackoverflow.com/questions/38897256
复制相似问题