【发布时间】:2019-11-14 07:45:15
【问题描述】:
我从 iOS 和 Android 应用程序的后端发送推送通知 我在推送请求中查看了以下内容:
Array ( [registration_ids] => Array ( [0] => test_token ) [notification] => Array ( [title] => 通知标题 [body] => 通知文本 [sound] => 1 [content_available ] => 1 [priority] => high [badge] => 4 [smallIcon] => small_logo [click_action] => .NoticeFullController ) [data] => Array ([short_text] => 通知短文本 [selectNotice] = > 123 [selectStatus] => 4 ) [apns] => Array ( [headers] => Array ( [apns-priority] => 5 ) [payload] => Array ( [aps] => Array ( [category] = > notice_push ) ) ) )
Android 应用没有问题,但在 iOS 上出现以下情况:
如果应用在后台或者通知关闭,会显示一个,但是点击后会打开应用的主页面,需要打开一个全视图的页面请求中'selectNotice'字段的ID大于0的通知,否则返回主页。
如果应用程序已打开,则在第一次启动时只有应用程序的声音,仅此而已。并且应该显示一个通知,而不仅仅是它的声音。
下次发送推送时,会显示两条通知,一条来自后台模式,第二条是我的代码创建的,但你只需要一条。并且当你点击它时,应用程序的主页面打开了,但是如果它大于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)
}
}
}
}
【问题讨论】:
标签: ios swift xcode push-notification firebase-cloud-messaging