Post

Replies

Boosts

Views

Activity

push notification on capacitor ios app with firebase or apnotic
Hi, I'm working on an IOS app using capacitor. I'm trying to receive push notifications on my downloaded app from testflight. I tried with FCM and it's working on my android app but not on ios and the logs show no error. This is how I retreive the FCM token: const fcmToken = await FCM.getToken() sendSubscriptionToBackEnd({ fcm_token: fcmToken.token, device: platform }) Then I have a job on my backend to send the push notifications: def perform fcm = FCM.new( StringIO.new(Rails.application.credentials.google_application_credentials), Rails.application.credentials.firebase_project_id ) NotificationSubscription.find_each(batch_size: 100) do |subscription| begin response = fcm.send_v1({ token: subscription.fcm_token, notification: { title: 'Un nouveau signal a été publié', body: 'Un nouveau signal a été publié, cliquez ici pour le voir' }, android: { priority: 'high' }, apns: { payload: { aps: { # alert: { # title: 'Un nouveau signal a été publié', # body: 'Un nouveau signal a été publié, cliquez ici pour le voir' # }, sound: 'default' } }, headers: { "apns-priority": "10", "apns-push-type": "alert" } } }) if response[:status_code] == 200 Rails.logger.info "Notification sent successfully to #{subscription.id} on device #{subscription.device}" else Rails.logger.error "Failed to send notification to #{subscription.id} body: #{response[:body]}" # subscription.destroy end rescue StandardError => e Rails.logger.error "Error while sending notification to #{subscription.device}: #{e.message}" subscription.destroy end end end and the logs show that it's successful but i dont receive the notification. When I test from firebase console I receive the push notification on both ios and android capacitor apps. I also added this in the apple delegate: Messaging.messaging().apnsToken = deviceToken Messaging.messaging().token(completion: { (token, error) in if let error = error { NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error) } else if let token = token { NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token) } }) } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error) } I also tried using apns tokens and ther apnotic gem: console.log('APNs Token:', token.value) if (platform === 'ios') { sendSubscriptionToBackEnd({ apns_token: token.value, device: platform }).then(() => { displaySnackbar(`APNs token: ${token.value}`, 'success') }) } }) # Create the APNs connection outside the loop connection = Apnotic::Connection.new( auth_method: :token, cert_path: StringIO.new(Rails.application.credentials.apns_key_path), key_id: Rails.application.credentials.apn_key_id, team_id: Rails.application.credentials.apple_team_id ) NotificationSubscription.find_each(batch_size: 100) do |subscription| if subscription.device == 'ios' begin # Create the notification for the current device token notification = Apnotic::Notification.new(subscription.apns_token) notification.alert = "Un nouveau signal a été publié" notification.topic = Rails.application.credentials.apple_bundle_id # Prepare and send the push push = connection.prepare_push(notification) push.on(:response) do |response| if response.ok? Rails.logger.info "Notification sent successfully to #{subscription.id} on device #{subscription.device}" else Rails.logger.error "Failed to send notification to #{subscription.id} body: #{response.status} - #{response.body}" end end connection.push_async(push) rescue StandardError => e Rails.logger.error "Error while sending notification to #{subscription.device}: #{e.message}" subscription.destroy end end end connection.join(timeout: 5) connection.close end but i have a bad token error: Failed to send notification to 223 body: 400 - {"reason"=>"BadDeviceToken"} I, [2025-01-23T02:23:59.013407 #104] INFO -- : [ActiveJob] [ApnsNotificationJob] I checked my aps entitlement env and its production, have all the certificates, keys.. so I dont understand why i can receive push notifications from firebase console but not from my app
2
0
614
Jan ’25