作者:凯米宝贝青岛Pacific早教中心 | 来源:互联网 | 2023-07-24 00:54
我从iOS和Android应用程序中的应用程序后端发送推送通知
我在推送请求中浏览了以下内容:
Array([registration_ids] => Array([0] => test_token)[notification] => Array([title] =>通知标题[body] =>通知文本[声音] => 1 [content_available ] => 1 [优先级] =>高[徽章] => 4 [smallIcon] => small_logo [click_action] => .NoticeFullController)[data] => Array([short_text] =>通知的短文本[selectNotice] = > 123 [selectStatus] => 4)[apns] =>数组([headers] => Array([apns-priority] => 5)[payload] => Array([aps] => Array([category] = > notice_push))))
Android应用程序没有问题,但是在iOS上会发生以下情况:
-
如果应用程序处于后台或关闭了通知,则会显示一个,但是单击该应用程序时,将打开应用程序的主页,您需要打开一个页面,其中包含如果请求的ID大于0,则返回请求中“ selectNotice”字段中ID的通知,否则返回主页。
-
如果应用程序处于打开状态,则在第一次启动时,只会显示应用程序的声音,仅此而已。并且应该显示通知,而不仅仅是声音。
-
下次发送推送时,将显示两个通知,其中一个是从后台模式发出的,第二个是由我的代码创建的,但您只需要一个。当您单击它时,将打开应用程序的主页,但是如果请求的请求大于“ 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)
}
}
}
}
您有两个通知,因为当您的应用程序位于前景中时,(?<=input name="__RequestVerificationToken" type="hidden" value=")(.*?")
会被调用,而当您的应用程序处于背景中时,您单击该通知时,就会被调用。
不推荐使用此方法。
您只需实现这两个委托方法。
cFYtO3f_MtOi9dulLEnZFpwu33iqrQcmVyIZ9ntsNeDtvPPMEk_-G5AubkrxUi8iqYtJ3yBymB0csk4lLGGZppevAkcHZWuOjjG87bFIPbs1"
在应用程序处于前台状态时被调用
当应用程序在后台单击通知时,会调用
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any])
。