【问题标题】:Where should I handle Push Notificaiotn while using GCM ios 9使用 GCM ios 9 时我应该在哪里处理推送通知
【发布时间】:2016-03-12 07:46:11
【问题描述】:

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

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

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

  1. 默认方法
- (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
     // ...
     }
    

现在我很困惑我应该在哪里处理我的Notificaiton

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

我应该在这两种方法中都写方法吗?

【问题讨论】:

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


    【解决方案1】:

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

    application:didReceiveRemoteNotification: 在应用程序打开并且您收到普通推送通知时调用。您通过通知中心收到警报的类型。

    application:didReceiveRemoteNotification:fetchCompletionHandler: 在服务器通知应用程序知道有东西要下载时被调用。您检查 userInfo 以了解要下载的内容,启动下载并在 NewData/NoData/Failed 时调用处理程序(UIBackgroundFetchResult)

    不确定 GCM 对这两种方法的作用,但根据这些信息,您应该能够弄清楚。

    【讨论】:

    【解决方案2】:

    你应该使用 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]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2018-01-02
      • 2017-05-24
      • 1970-01-01
      相关资源
      最近更新 更多