'Firebase Push Notifications are not received in iOS device?

I know this question has ben asked multiple times and I've been searching and testing all of them to no avail.

Things I've done so far:

  1. I have checked the bundle ID in firebase against Xcode and they match.
  2. I have checked the App has Push notifications permission in the apple developer for both Development and Productions.
  3. I have added the APNs Auth Key to the firebase App from the firebase console.

This is my Swift code for requesting Push Notifications permission which works fine and I get the device Token as as well:

import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications



class AppDelegate: NSObject, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions
      launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {

      // 1
    FirebaseApp.configure()
      // 2
    Messaging.messaging().delegate = self
      
    UNUserNotificationCenter.current().delegate = self
      
      
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]){ success, _ in
          
          guard success else{
              return
          }
          print("success in APNS")
      }

   
      application.registerForRemoteNotifications()
      
    return true
  }
    
    
    // set FirebaseMessaging service with apnsToken
      func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
          Messaging.messaging().apnsToken = deviceToken
      }
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        messaging.token { token, _ in
            guard let token = token else{
                return
            }
            print("Token: \(token)")
        }
        
    }
    
    
}

And this is my Payload code for sending the Push notes using PHP:

$devieToken = "fH_4w9wyWUtqkhOtW7OTR6:APA91bGfaUqS5usuloP3SBGjKQU08rbfkMaYpEoJ5cPUd1CRSSve0-vDJY4TiKs4M5gWdIO7HWeV6uecdb8mEnvJh1M_-i4RKrByG8BeK-oxfmDwA1cCPfwqLMBpP2veZL6ceqV-hBD_";
    
       $data = array('title' => 'Some Title', 'body' => 'Test message');


   //FCM API end-point
   $url = 'https://fcm.googleapis.com/fcm/send';
   //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
   $server_key = 'AAAACQ5Gg.....';

  $fields = array();
   $fields['notification'] = $data; 
    $fields['to'] = $devieToken;

   //header with content_type api key
   $headers = array(
   'Content-Type:application/json',
   'Authorization:key='.$server_key
   );
   //CURL request to route notification to FCM connection server (provided by Google)           
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);
   if ($result === FALSE) {
   die('Oops! FCM Send Error: ' . curl_error($ch));
   }
   curl_close($ch);
  echo $result;

when I run my payload code, I get this success message:

{"multicast_id":2689367701123139035,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1644098106179560%1a6349ae1a6349ae"}]}

But no push notification is received on my iPhone!!

What am I missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source