美文网首页iOS
iOS内使用谷歌推送相关的代码

iOS内使用谷歌推送相关的代码

作者: 陈藩 | 来源:发表于2022-02-15 08:36 被阅读0次

1.添加头文件

    import Firebase
    import FirebaseMessaging

2.遵守协议

class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate,MessagingDelegate{

3.appDelegate内的代码

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

   // 谷歌推送相关
    self.setUpFirebaseConfigure(application)
}

4.协议方法

    func setUpFirebaseConfigure(_ application:UIApplication){
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    
      // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.current().delegate = self

    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { ret, error in
            print("ret == \(ret)")
            print("error == \(error)")
    }
    application.registerForRemoteNotifications()
    
}

//获取token
internal func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
    print("deviceToken==\(deviceToken)")
    Messaging.messaging().apnsToken = deviceToken
    
}

//注册token
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
  print("Firebase registration token: \(String(describing: fcmToken))")

  let dataDict: [String: String] = ["token": fcmToken ?? ""]
  NotificationCenter.default.post(
    name: Notification.Name("FCMToken"),
    object: nil,
    userInfo: dataDict
  )
}

5.其他相关方法

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    let userInfo = notification.request.content.userInfo

}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
    let userInfor = response.notification.request.content.userInfo  
  
}

相关文章

  • iOS内使用谷歌推送相关的代码

    1.添加头文件 2.遵守协议 3.appDelegate内的代码 4.协议方法 5.其他相关方法

  • iOS10以上 Swift5.0 推送通知

    我们之前发过关于推送的文章iOS 推送通知及通知扩展,其中介绍了推送相关流程及代码实现,不过使用OC实现的,现在我...

  • 了解消息推送

    在开发过程中,IOS系统中已经内置消息推送,IOS开发者只需要根据IOS提供的方法规则去使用,其实谷歌也有自己的消...

  • Flutter-02 目录介绍

    一、目录介绍 android:android平台相关代码 ios:ios平台相关代码 lib:flutter相关代...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • iOS 谷歌推送服务

    简单记录一下集成谷歌推送服务的步骤: 一、谷歌推送平台部署 1、平台地址(需翻墙):https://console...

  • iOS推送相关

    1、清除推送的badgeNumber 在bugtags后台bug时发现,有用户因为下面的代码,而崩溃。 查找原因得...

  • iOS推送相关

    最近遇到需求: App未启动状态下,接收到推送,需要跳转到具体页面. 初始想法:在启动的时候,根据启动的原因.获取...

  • iOS应用内跳转系统推送设置页

    判断用户是否打开了推送 然后如果用户没有打开推送按钮跳转到应用相关设置页面 参考资料 iOS应用内跳转到指定系统设置页

  • iOS推送通知

    学习iOS开发已经两年多了,推送方面一直使用第三方极光推送,对推送没有进行系统的学习。今天我就把推送相关的知识点梳...

网友评论

    本文标题:iOS内使用谷歌推送相关的代码

    本文链接:https://www.haomeiwen.com/subject/kzaclrtx.html