我已经将我的应用程序设置为与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
状态并调用我的方法来处理它。
我应该在这两种方法中编写方法吗?
我不熟悉GCM,但是您列出的两种通知方法标准UIApplicationDelegate方法并处理不同的场景。
当应用程序打开并且您收到纯推送通知时,将调用application:didReceiveRemoteNotification:
。通过通知中心收到警报的类型。
当服务器让应用知道有内容要下载时,将调用application:didReceiveRemoteNotification:fetchCompletionHandler:
。您检查userInfo以获取要下载的内容,启动下载并在NewData/NoData/Failed时调用处理程序(UIBackgroundFetchResult)
不确定GCM对这两种方法的作用,但是有了这些信息,您应该能够弄清楚。
你应该使用 GCM 方法。
- (无效)申请
- :(UI申请*)申请didReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {}
在此方法中,您可以处理通知。例如
- (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]
}