How to block some firebase push notifications? - ios

I have some parameters. Server send push notifications every devices but if parameters are on it can see notification with devices.
I can send firebase push notification successful and I can get parameter but I cant block notification.
How I can do it?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
FirebaseApp.configure()
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
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()
if(messageType == "some parameter"){
}
// [END register_for_notifications]
return true
}

If you dont want to receive push notifications then you just need to deny
notification permission when your app ask for that.
Or you could disable notifications by opening Settings -> Notifications, and then turning that app's notifications off.

You can use topics to send and receive specific type of notification. You can see
topics.

Unfortunately, you cannot block showing remote push notifications on iOS device. As it was said the backend should handle this.

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)

Update deprecated Firebase functions in Swift

I have this code to push app notifications.
It works well but I get this warning:
'MessagingRemoteMessage' is deprecated: FCM direct channel is deprecated, please use APNs for downstream message handling.
and this one 'appData' is deprecated
I made a research on Google but didn't find anything to fix this.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
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().delegate = self
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
// The callback to handle data message received via FCM for devices running iOS 10 or above.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
The question is whether you are simply sending display/alert notifications and background notifications? Or are you using direct channel to send some hidden data message for real time updates?
MessagingRemoteMessage is data message object sent and handled through direct channel. If you only want to push notifications, and you also don't seem to enable direct channel in your code above. You can use Apple's API UNUserNotificationCenterDelegate to handle alert message or AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler: to handle background notifications.

Sending a push notification to iOS through a CLI function

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.

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.

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