Skip to content

Issue goes on with Cloud Messaging Push Notification to iOS device (in 5.17.0) #2438

@Sunrise17

Description

@Sunrise17

### Environment

  • Xcode version: 10.1
  • Deployment Target: 12.1
  • Firebase SDK version: 5.17.0 (Updated from 5.16.0)
  • Firebase Component: Core, Messaging
  • Component version: * Core 5.17.0, Messaging 3.3.1
    CocoaPods 1.6.0
    Using Bolts (1.9.0)
    Using BoringSSL-GRPC (0.0.2)
    Using FBSDKCoreKit (4.40.0)
    Using FBSDKLoginKit (4.40.0)
    Using Firebase (5.17.0)
    Using FirebaseAnalytics (5.6.0)
    Using FirebaseAnalyticsInterop (1.2.0)
    Using FirebaseAuth (5.3.1)
    Using FirebaseAuthInterop (1.0.0)
    Using FirebaseCore (5.3.0)
    Using FirebaseDatabase (5.1.0)
    Using FirebaseFirestore (1.0.1)
    Using FirebaseInstanceID (3.5.0)
    Using FirebaseMessaging (3.3.1)
    Using GTMSessionFetcher (1.2.1)
    Using Google-Mobile-Ads-SDK (7.40.0)
    Using GoogleAppMeasurement (5.6.0)
    Using GoogleUtilities (5.3.7)
    Using Protobuf (3.6.1)
    Using gRPC-C++ (0.0.6)
    Using gRPC-Core (1.17.0)
    Using leveldb-library (1.20)
    Using nanopb (0.3.901)

### Problem

I have problem when using FCM in iOS. When i run the code from XCode for the first time and fresh install app to the device, app starts receiving notification from Cloud Messaging Console and from my Python server without an issue. After i stop the code and run the app from the device, It stopped receiving any notification with following error below; If i delete the app and again re-install from Xcode, it starts receiving notification with new fcmToken, till i restart the app.

Received Error from Python Server;

{
  "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "status": "NOT_FOUND",
    "details": [
      {
        "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
        "errorCode": "UNREGISTERED"
      }
    ]
  }
}

Relevant Code:

Code in App AppDelegate.swift

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        //Firebase Setup
        FirebaseApp.configure()
        
        Messaging.messaging().delegate = self
        
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in } )
        application.registerForRemoteNotifications()
        
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        //Reset Bagde while entering App
        if (UIApplication.shared.applicationIconBadgeNumber != 0) {
            UIApplication.shared.applicationIconBadgeNumber = 0
        }
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("APNs token retrieved: \(deviceToken.hexString)")
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Unable to register for remote notifications: \(error.localizedDescription)")
    }   
}

extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        let userInfo = notification.request.content.userInfo
        print("userInfo \(userInfo)")
        
        completionHandler([.alert, .badge, .sound])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
        let userInfo = response.notification.request.content.userInfo
        print("userInfo \(userInfo)")
    
        completionHandler()
    }
    
}

extension AppDelegate: MessagingDelegate {
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }
    
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Received data message: \(remoteMessage.appData)")
    }
}

extension Data {
    public var hexString: String {
        var string = ""
        enumerateBytes { (buffer, _, _) in
            buffer.forEach({ (byte) in
                string = string.appendingFormat("%02x", byte)
            })
        }
        return string
    }
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions