使用 GCM 关闭应用程序时在 iOS 中接收推送通知
Receiving push notification in iOS when the app is closed using GCM
我的应用程序使用 GCM 接收推送通知。我的 android 应用程序运行完美,因为当我完全关闭该应用程序时,我仍然会收到通知,但是如果我对我的 iOS 版本执行相同操作,我将不会收到任何通知。
请注意,这当然是在应用程序首次注册然后关闭应用程序之后。我找不到很多关于这个的信息。有些帖子说你做不到。但是 facebook 和 facebook messenger 之类的公司都这样做。有谁知道这是怎么做到的?
这是我的
DidReceiveRemoteNotification method which is taken from the GCM documentation
// [START ack_message_reception]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
// [END_EXCLUDE]
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}
// [END ack_message_reception]
我发送的负载看起来像这样
$payload = array('registration_ids' => $Regids,
'content_available' => false,
'notification' => array("body"=> $_POST['message'],
"sound" => "default",
'badge' => '1'
)
);
请注意,如果应用程序在后台运行,此方法有效
当应用程序关闭时,它也适用于 iOS - 如果没有,则有问题。
但是,当应用程序被用户关闭(在任务切换器中向上滑动)时,您不能将 静默推送 与 iOS 一起使用 - 所以如果您的应用程序需要要在用户无需点击推送的情况下对推送做出反应,您无法在应用程序关闭时执行此操作。
我认为您没有在通知负载中指定 alert
键。当应用程序关闭时,通知负载的 alert
键中提供的消息将显示在设备上。
{
"aps" : { "alert" : "Message received from Bob" },
"acme2" : [ "bang", "whiz" ]
}
在上面的 payload 中检查 alert
键。 Message 从 Bob 收到的消息将在应用程序关闭时显示在 iPhone 的通知中心。
我通过将它添加到我的负载中来设法让它工作
"priority" => "high"
也许有人可以评论为什么这有效,因为我不知道
我的应用程序使用 GCM 接收推送通知。我的 android 应用程序运行完美,因为当我完全关闭该应用程序时,我仍然会收到通知,但是如果我对我的 iOS 版本执行相同操作,我将不会收到任何通知。
请注意,这当然是在应用程序首次注册然后关闭应用程序之后。我找不到很多关于这个的信息。有些帖子说你做不到。但是 facebook 和 facebook messenger 之类的公司都这样做。有谁知道这是怎么做到的?
这是我的
DidReceiveRemoteNotification method which is taken from the GCM documentation
// [START ack_message_reception]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
// [END_EXCLUDE]
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}
// [END ack_message_reception]
我发送的负载看起来像这样
$payload = array('registration_ids' => $Regids,
'content_available' => false,
'notification' => array("body"=> $_POST['message'],
"sound" => "default",
'badge' => '1'
)
);
请注意,如果应用程序在后台运行,此方法有效
当应用程序关闭时,它也适用于 iOS - 如果没有,则有问题。
但是,当应用程序被用户关闭(在任务切换器中向上滑动)时,您不能将 静默推送 与 iOS 一起使用 - 所以如果您的应用程序需要要在用户无需点击推送的情况下对推送做出反应,您无法在应用程序关闭时执行此操作。
我认为您没有在通知负载中指定 alert
键。当应用程序关闭时,通知负载的 alert
键中提供的消息将显示在设备上。
{
"aps" : { "alert" : "Message received from Bob" },
"acme2" : [ "bang", "whiz" ]
}
在上面的 payload 中检查 alert
键。 Message 从 Bob 收到的消息将在应用程序关闭时显示在 iPhone 的通知中心。
我通过将它添加到我的负载中来设法让它工作
"priority" => "high"
也许有人可以评论为什么这有效,因为我不知道