使用GCM ios 9时,我应该在哪里处理Push Notificaiotn

时间:2016-03-12 07:46:11

标签: ios objective-c push-notification google-cloud-messaging apple-push-notifications

我已将我的应用设置为与GCM配合使用。

我已成功添加代码以将GCM集成到我的应用程序中。

现在我有两种方法来处理推送通知:

  
      
  1. 默认方法
  2.   
- (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];

}
  1. 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
     // ...
     }
    
  2. 现在我很困惑我应该在哪里处理Notificaiton

    我应该在哪里检查application状态并调用我的方法来处理它。

    我是否必须在这两种方法中编写方法。

2 个答案:

答案 0 :(得分:1)

我不熟悉GCM,但是您列出了标准UIApplicationDelegate方法并处理不同场景的两种通知方法。

当应用程序打开并且您收到普通推送通知时,将调用

application:didReceiveRemoteNotification:。通过通知中心收到警报的类型。

当服务器让应用程序知道要下载的内容时,会调用

application:didReceiveRemoteNotification:fetchCompletionHandler:。检查userInfo以获取下载内容,启动下载并在NewData / NoData / Failed

时调用处理程序(UIBackgroundFetchResult)

不确定GCM对这两种方法的作用,但有了这些信息,你应该能够弄明白。

答案 1 :(得分:0)

您应该使用GCM方法。

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(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]
}