我已将我的应用设置为与GCM配合使用。
我已成功添加代码以将GCM集成到我的应用程序中。
现在我有两种方法来处理推送通知:
- 默认方法
醇>
- (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];
}
GCM方法
- (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
// ...
}
现在我很困惑我应该在哪里处理Notificaiton
。
我应该在哪里检查application
状态并调用我的方法来处理它。
我是否必须在这两种方法中编写方法。
答案 0 :(得分:1)
我不熟悉GCM,但是您列出了标准UIApplicationDelegate方法并处理不同场景的两种通知方法。
当应用程序打开并且您收到普通推送通知时,将调用 application:didReceiveRemoteNotification:
。通过通知中心收到警报的类型。
application:didReceiveRemoteNotification:fetchCompletionHandler:
。检查userInfo以获取下载内容,启动下载并在NewData / NoData / Failed
不确定GCM对这两种方法的作用,但有了这些信息,你应该能够弄明白。
答案 1 :(得分:0)
您应该使用GCM方法。
在此方法中,您可以处理通知。 例如,
- (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];
// [START_EXCLUDE]
if(application.applicationState == UIApplicationStateBackground){
//app is in background
}else if(application.applicationState == UIApplicationStateInactive){
//From background to foreground (user touchs notification)
}
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}