Sending a push notification to iOS through a CLI function - ios

I am attempting to use a CLI function to send a push notification to both android and iOS devices. When I use the iOS version, no notification is received when sent from my function. However, when I send them from the Firebase console, it will receive the notification but only while the application is open. I am thinking I am missing either one or more crucial steps in setup or my function does not have all the needed data in the payload.
My function is sending as follows:
return Promise.all([token]).then(result=>{
const payload = {
notification: {
title : likename + " liked your post!",
"priority" : "high"
}
};
console.log(token);
return admin.messaging().sendToDevice(token,payload);
});
I have my iOS application set up with a certificate and have implemented my APP delegate as follows:
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Any help is greatly appreciated and please let me know if you need additional information.

After hours of reflection, I realized I had only implemented code to respond to the onLaunch or onResume versions. I had to implement those methods to my code and now have success.

Related

flutter 2 fcm ios apn issue

The firebase FCM is not working for me in flutter 2.2.2, am trying to implement push notification in my ios app and am getting this error :
8.3.0 - [Firebase/Messaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '*************'.
Notifications to this FCM Token will not be delivered over APNS.Be
sure to re-retrieve the FCM token once the APNS device token is set.
It’s been one month since this problem occurred.
I followed the step in this article and article
the push notifications works fine in Android the problem is with the iOS
this is my AppDelegate.swift
import UIKit
import Flutter
import Firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Use Firebase library to configure APIs
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
you have to add
Messaging.messaging().apnsToken = deviceToken
before
return super.application(application, didFinishLaunchingWithOptions: launchOptions)

How to handle Flutter FCM push notification when app in foreground for iOS?

I'm using local notification when app in foreground, on iOS still cannot get notification when app in foreground but on Android it works perfectly.
The problem is how to handle push notification when app in foreground for iOS ?
This my AppDelegate.swift :
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
I assume you're using the flutter_local_notifications plugin for local notifications and firebase_messaging for push notifications. As of right now, these two plugins do not work together. This is documented in the readme of the first plugin, and the issue is being tracked on both plugins. You'll just have to wait for the pull request on firebase_messaging to be merged.
See this issue for more details.

Firebase Cloud messaging doesn't work in Apple TestFlight

I've programmed a new App in Swift including Firebase.
When I run the App via Simulator, the Cloud Messaging (Push Notifications) works. But as I submitted it to TestFlight, no Message has appeared.
I've created all Certificates (for Development and Production) to send Push Notifications.
I think theres something with the token but I have no clue what :/ Can someone help me?
Here is my Code (AppDelegate.swift)
import UIKit
import CoreData
import Firebase
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:Any]?) -> Bool {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM
Messaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}

how to place Notification Authorization Request for FCM outside AppDelegate?

when we want to implement Firebase Cloud Messaging, we first have to ask permission for notification to the user. from Firebase Documentation they recommend this lines of codes to be implemented in Appdelegate on didFinishLaunchingWithOptions like below :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
but if place those codes in appDelegate, the pop up alert to ask permission will show up immediately after the user open up my app.but I want to show the alert in the certain View Controller.
if I move those code to a VC, then the application is not available, since the application comes from didFinishLaunchingWithOptions. and then the messagingDelegate ... I am not sure. could you please help me to rewrite those codes but if it is not in AppDelegate?
Add a Class
import Foundation
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications
class Notifications {
private init() { }
static let shared = Notifications()
func requestNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isAuthorized, error) in
if error != nil {
print(error as Any)
return
} else {
print("set up successfully")
// Completion Code Here:
}
}
}
}
Add to your prefered VC
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Notifications.shared.requestNotifications()
}
I was testing on a iOS 10.3.3 device and it didn't register until I set the delegate of the UNUserNotificationCenter to UIApplication.shared.delegate as? UNUserNotificationCenterDelegate implementing delegate in the AppDelegate as an empty delegate. But it didn't work implementing an empty delegate in the current view controller.
AppDelegate.swift:
extension AppDelegate: UNUserNotificationCenterDelegate { }
So here's the method in my view controller:
private func registerPushNotifications() {
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {granted, _ in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
})
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}
Of course you have to leave FirebaseApp.configure() in didFinishLaunchingWithOptions method.
Later edit: You might get a warning about calling registerForRemoteNotifications() from the main thread, so I adjusted the code to do that.

registerUserNotificationSettings is not firing delegates and push notification not working my project

I did all the procedures written in the documentation. That is my code. When I added breakpoint, this delegates was not called.
private func pushNotificationHandler(_ application: UIApplication){
FirebaseApp.configure()
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
UIApplication.shared.applicationIconBadgeNumber = 0
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// program never come here
print(deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// program never come here
print(error)
}
edit: tested in iphone 6 is working done, but iPhone 11 pro max , iPhone XS Max And iPhone 7 are the same problem.
i think you need to add
FirebaseApp.configure()
Messaging.messaging().delegate = self
in Appdelegate->didFinishLaunchingWithOptions
I solved the problem. The internet connection of the phone cannot connect with apple push servers. After 10 hours, I changed the DNS and solved the problem.

Resources