热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我无法接收iOSswift3的数据通知-IamnotabletoreceivedatanotificationiOSswift3

Iamfollowingsamplerprojectprovidedbyfirebase.FirebaseCloudMessagingsammple我正在关注firebase提

I am following sampler project provided by firebase. Firebase Cloud Messaging sammple

我正在关注firebase提供的sampler项目。 Firebase云消息传递

My app delegate is

我的app代表是

import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
import FirebaseInstanceID

//add firebase code app delegate code

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizatiOnOptions= [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })

    } else {

        let settings: UIUserNotificatiOnSettings=
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)

        }

    application.registerForRemoteNotifications()

    // [END register_for_notifications]
    FirebaseApp.configure()

    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]

    return true

}

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    completionHandler(UIBackgroundFetchResult.newData)
}
// [END receive_message]


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Unable to register for remote notifications: \(error.localizedDescription)")
}

// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the InstanceID token.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("APNs token retrieved: \(deviceToken)")

    // With swizzling disabled you must set the APNs token here.
    InstanceID.instanceID().setAPNSToken(deviceToken, type: InstanceIDAPNSTokenType.sandbox)

}


}


        // [START ios_10_message_handling]
        @available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    // Change this to your preferred presentation option
    completionHandler([.alert,.badge,.sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    completionHandler()
}
}
// [END ios_10_message_handling]

extension AppDelegate : MessagingDelegate {

// [START refresh_token]
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {

    print("Firebase registration token: \(fcmToken)")

    print(fcmToken)

    resgisterNotificationToken(fcmToken: fcmToken)

}
// [END refresh_token]

func application(received remoteMessage: MessagingRemoteMessage) {

    //get called when sending notification from POSTMAN and when app is open

    print("%@", remoteMessage.appData)

    print("%@", remoteMessage)

}


func resgisterNotificationToken(fcmToken:String){

    //let deviceId = UIDevice.current.identifierForVendor!.uuidString

    //let parameters = ["OTY": AppConstants.init().OS_TYPE,"REGID": fcmToken] as Dictionary



}

}

I can receive notification which was sent from firebase console. i have upgraded my firebase library to latest 3.0 something.

我可以收到从firebase控制台发送的通知。我已将我的firebase库升级到最新的3.0版本。

also i am getting following warning. 'InstanceIDAPNSTokenType' is deprecated: Use FIRMessaging's APNSToken property instead.

我也得到了警告。不推荐使用“InstanceIDAPNSTokenType”:改为使用FIRMessaging的APNSToken属性。

kindly provide solution with code and give me server request structure so that i can test it from postman.

请提供代码解决方案,并给我服务器请求结构,以便我可以从邮递员测试它。

Thanks in advance.

提前致谢。

2 个解决方案

#1


9  

Can you try setting the apn token like this:

你能尝试像这样设置apn令牌:

FIRInstanceID.instanceID()
    .setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.unknown)

FIRInstanceID setAPNSToken

FIRInstanceID setAPNSToken

Set APNS token for the application. This APNS token will be used to register with Firebase Messaging using token or tokenWithAuthorizedEntity:scope:options:handler. If the token type is set to FIRInstanceIDAPNSTokenTypeUnknown InstanceID will read the provisioning profile to find out the token type.

为应用程序设置APNS令牌。此APNS令牌将用于使用令牌或tokenWithAuthorizedEntity:scope:options:handler向Firebase Messaging注册。如果令牌类型设置为FIRInstanceIDAPNSTokenTypeUnknown InstanceID将读取配置配置文件以找出令牌类型。

Firebase API reference

Firebase API参考

It's working for me!

它对我有用!

EDIT:

编辑:

With Firebase version 4.0.0 the way to do it has changed:

使用Firebase 4.0.0版本的方法已经改变:

Messaging.messaging()
    .setAPNSToken(deviceToken, type: MessagingAPNSTokenType.unknown)

FIRMessaging API reference

FIRMessaging API参考

#2


8  

I've just updated the Github sample app to reflect the API changes. Sorry about that. I think some of the changes slipped through. The preferred way to set the APNs token (if you have disabled swizzling) is now:

我刚刚更新了Github示例应用程序以反映API更改。对于那个很抱歉。我认为其中一些变化已经过去了。设置APN令牌的首选方法(如果您已禁用调配)现在是:

Messaging.messaging().apnsToken = deviceToken

The old method, setAPNSToken:type: was causing more confusion because if the type was included and it did not match the type of build, the FCM token would not work. If you do need to use the old method, I'd recommend using the 'Unknown' enum, which will do an automatic check.

旧方法setAPNSToken:type:引起了更多混淆,因为如果包含类型并且它与构建类型不匹配,则FCM令牌将无法工作。如果您确实需要使用旧方法,我建议使用“未知”枚举,它将进行自动检查。

Your question title mentioned that you're not receiving data messages and the new sample change should show that. The way to receive data messages is:

您的问题标题提到您没有收到数据消息,新的示例更改应该显示。接收数据消息的方法是:

  1. Enable the direct channel by setting: Messaging.messaging().shouldEstablishDirectChannel = true

    通过设置启用直接通道:Messaging.messaging()。shouldEstablishDirectChannel = true

  2. Implement the FIRMessagingDelegate and the messaging:didReceiveRemoteMessage method.

    实现FIRMessagingDelegate和messaging:didReceiveRemoteMessage方法。

Another sample app you can look at is part of the open-source FCM repo here.

您可以看到的另一个示例应用程序是此处开源FCM repo的一部分。


推荐阅读
author-avatar
HuPangpang_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有