Updated 28 February 2023
Hello Guys, Today we learn about “How to Unsubscribe topic for notification in swift“.
Sometimes we want to push the notification stores-wise into our app. For example, If I am using multiple stores like Arabic, English, French e.t.c and want to push the notification to the customer according to store. means if I am using an English store then I receive only English store-related notifications. for this, we use the subscribe and unsubscribe token functionality.
Integrate Firebase push notification into your app notifications
For firebase push notification configuration, please go through this link, Please click here.
Here you can find how can we integrate the firebase notification into the app.
Now, apply the code in your project where you are using the store selection functionality.
when you switch the store from x to y you need to add below mentioned code.
1 2 3 4 5 6 7 8 9 10 11 |
Messaging.messaging().deleteToken(completion: { error in print(error) Messaging.messaging().unsubscribe(fromTopic: "Your previous stored topic") { error in print(error) var topic = "Your new Store topic" Messaging.messaging().subscribe(toTopic: topic) let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate appDelegate?.setupPushNotification1(application: (appDelegate?.application)!, launchOptions: appDelegate?.launchOptions) } }) |
Explanation:- To unsubscribe from the notification we need to delete the registered token first, on completion we will unsubscribe from the added topic, on completion we will again subscribe to the new topic. and after that, we need to call AppDelegate Function where we registered our topic and token. so that we can register the token and topic for notification.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
func setupPushNotification1(application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]?){ //Push NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: Notification.Name.MessagingRegistrationTokenRefreshed, object: nil) if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self Messaging.messaging().delegate = self let Options: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: Options, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() if let remoteNotif = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] { DispatchQueue.main.asyncAfter(deadline: .now() + 1.1, execute: { self.application(application, didReceiveRemoteNotification: remoteNotif) }) } } |
Important note, you need to do the changes from the server side too, your server team also need to configure the notification according to the topic from the admin panel
In this blog, we discussed, “How to Unsubscribe topic for notification in swift”.
I hope this blog will help you to understand the notification unsubscribe ams subscribe flow
To learn more about iOS-related topics, please click here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.