【问题标题】:Firebase background and inactive notifications not showing iOSFirebase 背景和非活动通知未显示 iOS
【发布时间】:2017-04-27 04:44:32
【问题描述】:

在 iOS 10 中使用 FCM 推送通知:

这是通过我们自己的 API 推送通知后被调用的 snipper:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification

    [[FIRMessaging  messaging] appDidReceiveMessage:userInfo];
    if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }

    completionHandler(UIBackgroundFetchResultNewData);
    if(application.applicationState == UIApplicationStateBackground){
        [[TWMessageBarManager sharedInstance] showMessageWithTitle:@"Bacgkround"
                                                       description:@"Wassup"
                                                              type:TWMessageBarMessageTypeSuccess callback:^{


                                                              }];

    }
    else if(application.applicationState == UIApplicationStateActive){
        [[TWMessageBarManager sharedInstance] showMessageWithTitle:@"Active"
                                                       description:@"Wassup"
                                                              type:TWMessageBarMessageTypeSuccess callback:^{


                                                              }];
    }

    else if(application.applicationState == UIApplicationStateInactive){
        [[TWMessageBarManager sharedInstance] showMessageWithTitle:@"InActive"
                                                       description:@"Wassup"
                                                              type:TWMessageBarMessageTypeSuccess callback:^{


                                                              }];
    } 
}

当应用程序处于活动状态时,将调用上述方法并执行 UIApplicationStateActive 案例,并且我正在使用 3rd 方库显示一个弹出窗口。当我按下主页按钮并推送通知时,会调用上述方法并执行 UIApplicationStateBackground 但我不确定如何以横幅的形式显示通知?

这是我必须处理通知的方法吗?如果是,我该如何处理 Background 和 Inactive 状态?

【问题讨论】:

    标签: ios objective-c firebase firebase-cloud-messaging uiapplication


    【解决方案1】:

    您只需将通知正文放入您从服务器收到的有效负载中,如果您不添加它,iOS 会将其视为静默推送通知,并且不会显示通知横幅。例如

    {  
       "data":{  
      "title":"mytitle",
      "body":"mybody",
    },
    "notification":{  
      "title":"mytitle",
      "body":"mybody",
     },
    }
    

    当用户在任何状态的应用程序中单击通知横幅时,您将触发其回调方法,由 firebase.e.g. 提供。

    // [START receive_message]
     - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
    

    等等

    注意:无需使用任何第三方库来显示通知横幅,因为这一切都由 iOS 控制。

    【讨论】:

      【解决方案2】:

      我对@9​​87654321@ 了解不多,但我可以告诉您,当您的应用不在前台时,您不能使用任何库或您自定义的任何内容来显示通知。如果您的应用在后台并且您收到推送通知,则通知横幅将由 iOS 自行显示

      【讨论】:

      • 但是我需要在 UIApplicationStateBackground 案例中写一些东西对吗?
      • 我不这么认为,我从来没有为UIApplicationStateBackground处理推送通知做过任何事情,iOS会自己处理
      • 在这种情况下, - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {} 方法没有在我的案例!
      • 如果您的应用程序被暂停或终止,则可能会发生这种情况,因为您的委托/覆盖方法都不会被调用,iOS 将根据您要求的权限显示通知横幅或警报
      • 我已经尝试使用在线可用的 APNS 测试仪,一切正常,但在 FCM 中,我在后台/非活动状态下没有收到通知。所有权限都设置正确。
      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多