iOS do not call didReceiveRemoteNotification or didReceive events when receives a message

2,353 views
Skip to first unread message

Flavio

unread,
Dec 15, 2016, 11:55:15 AM12/15/16
to Firebase Google Group

Hi, I know that this topic was posted thousands of times by other users. I know this bacause I read all of those posts. ;-)
But until now I don't know why my app does not get notified when the phone receives a message.

I can tell that the messages arrives because I can see it on Firebase debug log as you can see below:

%@ [AnyHashable("notification"): {
    body = "Server Test";
    e = 1;
    sound = default;
    sound2 = default;
    title = Test;
}, AnyHashable("from"): 863402413932, AnyHashable("message"): Notification from Java application, AnyHashable("collapse_key"): com.itrax.VanCool]


I tried send messages via my server and via Firebase Console and I get the same result. Does not matter if the app is in foreground or background mode, I got nothing.

I'm testing on iOS Simulator running iOS 10.1, XCode 8 and Swift 3

Here is Swift my code

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

        initFirebade(application: application)
        
        return true
    }

    func initFirebade(application: UIApplication){
        if #available(iOS 10.0, *) {
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
            
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            // For iOS 10 data message (sent via FCM)
            FIRMessaging.messaging().remoteMessageDelegate = self
            
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        
        application.registerForRemoteNotifications()
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: NSNotification.Name.firInstanceIDTokenRefresh,
                                               object: nil)
 
        connectToFcm()
        
    }
    
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
        
        // set firebase apns token
        FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.sandbox)
    }
    func tokenRefreshNotification(_ notification: Notification) {
        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("InstanceID token: \(refreshedToken)")
            Utils.setFirebaseToken(firebaseToken: refreshedToken)
            FIRMessaging.messaging().subscribe(toTopic: "/topics/ledices")

        }
        
        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    }

    func connectToFcm() {
        FIRMessaging.messaging().connect { (error) in
            if error != nil {
                print("Firebase Unable to connect with FCM. \(error)")
            } else {
                print("Firebase Connected to FCM.")
                FIRMessaging.messaging().subscribe(toTopic: "/topics/ledices")

            }
        }
    }


    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        print("Firebase message 1")
        print(notification)
    }
    
 

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

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        print("Firebase register settings")
    }
    
    func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("Firebase background fetch")
    }


Here is my Java code for the server:

public static void sendMessage(String destinatary,String stMessage) throws IOException{
FCMSender sender=new FCMSender(SERVER_KEY);
Notification notification = new Notification.Builder(null)
.body("Server Test")
.title("Test").build();
Message message = new Message.Builder()
                .collapseKey("message")
                .timeToLive(60)
                
                //.delayWhileIdle(true)
                .priority(Priority.HIGH)
                .addData("message", "Notification from Java application")
                .notification(notification)
                .build();
                

// Use the same token(or registration id) that was earlier
// used to send the message to the client directly from
// Firebase Console's Notification tab.
System.out.println(destinatary);
Result result = sender.send(message,destinatary,5);
        System.out.println("Error: "+result.getErrorCodeName());
System.out.println("Success: "+result.getSuccess());
System.out.println("Message Id: "+result.getMessageId());
}


As I said I'd test sending message via Firebase Console and Server and I got nothing. I get no notifications when the app is foreground and background.

Here is my Xcode capabilities



I appreciate any help. I'm about to give up Firebase because I'm almost one week on this integration process and I have advanced very little. I thnk that Firebase should review its tutorial regarding iOS integration. It is deprecated and results on a non funcitional code. If I'd advance something is because of information that I got from posts of other pragrammers not Firebase documentation.

Thank you very much.
Flavio

Ian Barber

unread,
Dec 22, 2016, 8:18:55 PM12/22/16
to Firebase Google Group
Skimming the code it looks like: 

* applicationReceivedRemoteMessage is missing
* FIRMessaging.messaging.setRemoteMessageDelegate is missing

For a data message on an iOS 10 deploy target, thats the method you need. 

mohammad shalhoob

unread,
Oct 11, 2017, 2:04:16 PM10/11/17
to Firebase Google Group
notification not work on Simulator  , try in real device .
Reply all
Reply to author
Forward
0 new messages