为什么只有一个推送式iOS而只有两个通知?
我从iOS和Android应用程序的后端发送推送通知 我在推送请求中查看了以下内容: 数组([registration\u ids]=>Array([0]=>test\u token)[notification]=>Array([title]=>title of notification[body]=>Text of notification[sound]=>1[content\u available]=>1[priority]=>high[badge]=>4[smallIcon]=>small\u logo[点击动作]=>。NoticelController)[数据]=>Array([短文本]=>通知短文本[selectNotice]=>123[selectStatus]=>4[APN]=>Array([headers]=>Array([apns priority]=>5)[payload]=>Array([aps]=>Array([category]=>notice\u push))) Android应用程序没有问题,但在iOS上会发生以下情况:为什么只有一个推送式iOS而只有两个通知?,ios,swift,xcode,push-notification,firebase-cloud-messaging,Ios,Swift,Xcode,Push Notification,Firebase Cloud Messaging,我从iOS和Android应用程序的后端发送推送通知 我在推送请求中查看了以下内容: 数组([registration\u ids]=>Array([0]=>test\u token)[notification]=>Array([title]=>title of notification[body]=>Text of notification[sound]=>1[content\u available]=>1[priority]=>high[badge]=>4[smallIcon]=>small
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应用程序(u应用程序:UIApplication,didReceiveMemotentification用户信息:[AnyHashable:Any])
,当您的应用程序位于后台时单击通知时调用
此方法已弃用
您只需实现这两个委托方法
func userNotificationCenter(center:unuservificationcenter,willPresent通知:UNNotification,with completionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void)
在应用程序位于前台时调用
func-userNotificationCenter(center:unuservificationcenter,didReceive-response:UNNotificationResponse,withCompletionHandler completionHandler:@escaping()->Void)
在应用程序处于后台时单击通知时被调用。谢谢。当我单击通知并将数据从“selectNotice”传递给所选页面时,如何启动该页面?谢谢