【问题标题】:Hide APN notification ONLY when app is in background?仅在应用程序处于后台时隐藏 APN 通知?
【发布时间】:2023-03-06 22:46:01
【问题描述】:

我将 Firebase 云消息传递用于我的推送通知,它在其后端使用 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')
    );

【问题讨论】:

  • 你得到答案了吗?不可能吗?

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


【解决方案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)types 时,您应该指明适当的参数类型。如果您想在系统收到您设备的推送通知时显示警报,那么您应该传递 UIRemoteNotificationTypeAlert

您还可以组合类型并将其传递到该参数中:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge。

如果您不想从顶部收到警报,请删除警报。

希望对您有所帮助。

谢谢。

【讨论】:

  • 这个方法在我的代码中是空的,我自己没有显示任何东西,APN生成并显示通知
  • 你说的是来自iOS顶部的通知中心的通知吗? @Gintas_
  • @Gintas_,现在检查答案,您可以在注册时设置您想要的通知类型。因此,您可以选择不选择警报,删除警报。
  • 但是当应用程序被杀死时我需要提醒。只是不是在后台时
  • 你能在终止前尝试改变类型吗,比如说 didAppTerminate 方法...
猜你喜欢
  • 2018-08-04
  • 2019-02-25
  • 2020-10-22
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多