仅在应用处于后台时隐藏APN通知?

时间:2016-08-20 18:45:27

标签: ios swift push-notification apple-push-notifications firebase-cloud-messaging

我正在使用Firebase Cloud Messaging进行推送通知,该推送通知在后端使用APN。

当应用位于前景时,会调用didReceiveRemoteNotification并且不会显示通知,这是完美的。

当应用程序被杀时,didReceiveRemoteNotification未被调用,但会显示通知,也没关系。

但是,当应用程序位于后台时,会调用didReceiveRemoteNotification并显示通知。如何隐藏它并仅保留didReceiveRemoteNotification被叫?

以下是我的有效负载的样子:

$fields = array(
     'registration_ids' => $tokens,
     'data' => $message,
     'content_available' => true,
     'priority' => 'high',
     'notification' => array('body' => 'notifbody', 'title' => 'testtitle', 'sound' => 'default')
    );

1 个答案:

答案 0 :(得分:1)

试试这个。

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
      UIApplicationState state = [application applicationState];
        // user tapped notification while app was in background
    if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
         // go to screen relevant to Notification content
    } else {
         // App is in UIApplicationStateActive (running in foreground)
         // perhaps show an UIAlertView
    }
}

其次, 当你调用方法 - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)类型时,你应该指出适当的参数类型。如果您希望在系统收到设备的推送通知时显示提醒,则应传递 UIRemoteNotificationTypeAlert

此外,您可以组合类型并将其传递给该参数: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge

如果您不想从顶部收到提醒,请删除提醒。

希望它会对你有所帮助。

感谢。