macOS Remote Push Notifications Not Showing Alert Banner
In iOS the following code works fine:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
For macOS I changed that code to be:
UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }
DispatchQueue.main.async {
NSApplication.shared.registerForRemoteNotifications()
}
}
Turns out the line NSApplication.shared.registerForRemoteNotifications()
is incorrect. On macOS you have to pass in the same options you provided into this call.
Changing that line to the following worked.
NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])
What I find strange is that in Apple's documentation it says that method is deprecated, and that we should use registerForRemoteNotifications()
instead. Which makes me think there is some type of bug with registerForRemoteNotifications()
where the notifications are not appearing correctly.
One other thing to mention. It did take a little bit of time (couple minutes), and a few notifications sent, for them to actually appear after making that change. Not sure if it was just due to slow internet connection or what. But now they are appearing very quickly after being sent.
Edit
Apple has informed me that this is fixed in macOS 10.14.4. I have not been able to upgrade to the best and test it yet tho. So I can not confirm at this point. I will update this when I get a chance to test on macOS 10.14.4.
I can't get push notifications to appear in alert or banner form
Thank you for the suggestions but the problem has been resolved, it was an issue with the server payload
1 Our server is using a third party push notification library
2 this library was wrapping our json payload
Our payload
aps: {
alert: ...,
With another layer
aps: {
alert: {
aps: {
alert: ...,
Apple's APNs wasn't complaining one bit because the json is not malformed and totally legitimate and within size
I found this out after looking at the incoming notifications, while my iphone was run from/connected to Xcode.
Human error, server side
I am getting notification as banner when my app is open. I don't want to show banner when my app is open
Here you set presentation style alert that's why showing banner. You should pass UNNotificationPresentationOptionNone in completion handler as per apple document.
https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter?language=objc
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
//Called when a notification is delivered to a foreground app.
NSDictionary *userInfo = notification.request.content.userInfo;
completionHandler(UNNotificationPresentationOptionAlert);
}
Not Showing banner only vibrate device when push notification received
Finally i am resolve this issue. their issue on php code not in iOS code. in php code they are missing alert Keyword in aps array that why device only vibrate when receive Notification.
Apple Push Notification Service (APNS) not showing buttons (OSx App)?
By default "Notification style" is banner on OSx. Only for "Alert" style It will show buttons. So changing to "Alert" it starts showing buttons.
Handle Push Notification before it is displayed in the Notifications Banner
You can create "silent" push notifications. (See Apple documentation) When you receive such a silent notification:
- parse the JSON
- decide if you want to show it or not
- if yes, just show a normal local notification, it looks same as a push-notification for the user
You will find plenty of SO tutorials on how to create local notifications.
You have to implement application:didReceiveRemoteNotification: fetchCompletionHandler.
Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
registerForRemoteNotifications not working at all in Big Sur?
This was only broken in the development environment. It was always working as expected in production environment, but it was of course difficult to develop and test with this.
This was fixed in one of the minor versions of Big Sur. I am now using 11.5.1 and it is working now as expected in all environments.
Displaying a stock iOS notification banner when your app is open and in the foreground?
iOS 10 adds the UNUserNotificationCenterDelegate
protocol for handling notifications while your app is in the foreground.
The
UNUserNotificationCenterDelegate
protocol defines methods for receiving notifications and for handling actions. When your app is in the foreground, arriving notifications are delivered to your delegate object instead of displayed automatically using the system interfaces.
Swift:
optional func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
Objective-C:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
The UNNotificationPresentationOptions flags allow you to specify UNNotificationPresentationOptionAlert
to display an alert using the text provided by the notification.
This is key as it allows you to display the alert while your app is open and in the foreground, which is new for iOS 10.
Sample code:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().delegate = self
return true
}
}
// Conform to UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(.alert)
}
}
Related Topics
Swift Pattern Match on Array<Any>
How to Get The Range of The First Line in a String
Wkwebview - Update HTML Tags from Swiftui Textfields
Swift-Animate Cashapelayer Stroke Color
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property
Firebase Data Retrieval - Edit Annotation
Use Tableviewcontroller Inside Skscene
Not Getting Screenlock Notification on Swift 4 on Mac
Display Array in a Label in Swift
Uibutton State Changing While Scrolling The Tableview with Multiple Sections - Swift
How to Programmatically Scroll iOS Wkwebview, Swift 4
Create Objects/Instances in Variables Swift
Osx/Swift: Call Function at a Specific Date/Time
Nsdocumentcontroller.Opendocument Not Allowing Selection of Custom File Type
How to Draw Something on a PDF in Swift
How to Observe Torchlevel in Swift
Swift Dictionary Initialization of Custom Type Gives: '>' Is Not a Postfix Unary Operator Error