为什么iOS中只有一个推送而只有两个通知



我从iOS和Android应用程序中的应用程序后端发送推送通知我在推送请求中查看了以下内容:

Array([registration_ids]=>Array([0]=>test_token([通知]=>Array([标题]=>通知标题[正文]=>通知文本[sound]=>1[content_available]=>1[priority]=>high[徽章]=>4[smallIcon]=>small_logo[click_action]=>.NoticeFullController([data]=>Array=>4([amps]=>阵列([headers]=>阵列

安卓应用程序没有问题,但在iOS上会发生以下情况:

  1. 如果应用程序在后台或通知已关闭,则会显示一个,但当您单击它时,会打开应用程序的主页,并且如果ID大于0,则需要打开一个页面,其中包含请求中"selectNotice"字段中ID的通知的完整视图,否则为主页。

  2. 如果应用程序是打开的,那么在第一次启动时只有应用程序的声音,没有其他声音。并且应该显示一个通知,而不仅仅是它的声音。

  3. 下次你发送推送时,会显示两个通知,其中一个来自后台模式,第二个是由我的代码创建的,但你只需要一个。当你点击它时,应用程序的主页就会打开,但如果它大于0,你需要打开带有请求中"selectNotice"字段ID的通知的完整视图的页面,否则就是主页。

帮助处理iOS中的推送。事先非常感谢。我的AppDelegate代码:

import UserNotifications
import Firebase
import FirebaseMessaging
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Use Firebase library to configure APIs
FirebaseApp.configure()
// Уведомления в фоне
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})
// Override point for customization after application launch.
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
//Handle the notification
completionHandler(
[UNNotificationPresentationOptions.alert,
UNNotificationPresentationOptions.sound,
UNNotificationPresentationOptions.badge])
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
Messaging.messaging().subscribe(toTopic: "notice_push")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
let content = UNMutableNotificationContent()
if let title = userInfo["title"]
{
content.title = title as! String
}
if let short_text = userInfo["short_text"]
{
content.body = short_text as! String
} else if let short_text = userInfo["body"]
{
content.body = short_text as! String
}
if let badge = userInfo["badge"]
{
UIApplication.shared.applicationIconBadgeNumber = badge as! Int
}
//category = ".NoticeFullController";
content.userInfo = userInfo
content.sound = .default()
content.threadIdentifier = "my-notice"
if #available(iOS 12.0, *) {
content.summaryArgument = "notification"
} else {
// Fallback on earlier versions
}
UNUserNotificationCenter.current().delegate = self
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.5, repeats: false)
let request = UNNotificationRequest(identifier:"notice", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let getError = error {
print(getError.localizedDescription)
}
}
}
}

您有两个通知,因为当您的应用程序在前台时会调用func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]),当您在后台时单击通知时会调用。

此方法已弃用。

您只需要实现这两个委托方法。

当应用程序处于前台时调用func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

当应用程序处于后台时,单击通知时会调用func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

相关内容

  • 没有找到相关文章

最新更新