现在我有1种方法来做推送通知。
我使用firebase控制台(https://console.firebase.google.com/),然后分配包ID并发送推送通知。
现在我有一个问题。
如何在我的应用程序上使用firebase控制台进行检测(使用swift)。
这是我的didReceiveRemoteNotification代码
didReceiveRemoteNotification Code
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID1: \(messageID)")
}
var pushId = userInfo["push-id"] as? String
if(pushId == nil){
pushId = "-1"
}
fcmAccessCount(pushId: pushId!)
badgeCount()
switch application.applicationState {
case .active:
break
case .inactive:
break
case .background:
break
default:
break
}
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? String {
let alert = UIAlertController(title: "message is... ", message: alert, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
}
请在App Delegate类中编写此代码,如下所示,并尝试 -
class AppDelegate: UIResponder, UIApplicationDelegate{
}
@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
//From here we will navigate to other screens:-
print("User Info : \(userInfo)")
// Change this to your preferred presentation option
//completionHandler([])
completionHandler([.alert,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// From here we can navigat to required screens:-
// Print full message.
print(userInfo)
completionHandler()
}
}
extension AppDelegate : FIRMessagingDelegate {
/// The callback to handle data message received via FCM for devices running iOS 10 or above.
public func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
}