-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Native local notifications in macOS (is this already possible?) #2122
Comments
PushNotificationsIOS should work? At least, whenever I have touched, I've made sure the code compiles on macOS :) |
I tried it out with mixed results edit: actually at that time i may have also had expo installed... let me try again now that i have transitioned away from expo and towards |
Oh sorry, I meant the PushNotificationIOS that ships with React Native, AKA: |
ah ok - so you are saying this fork of |
if that's so then is this deprecated guide to push notifications still a good reference for how to handle notifications in macOS? for what it's worth i'm only interested in local machine notifications, not remote push notifications. not sure if that makes any difference / makes things easier or harder. |
I'm trying to follow (more or less) those deprecated instructions... When I go to add
(or Bear with me here - i'm a very experienced developer but I have fully 0 experience with Xcode or objective-C and it's been years since i did anything in C or C++ - but when I went looking for the
Given that my
That lead me to
(That's just a piece of the file; can post the whole thing.) Now I don't really know what I'm doing here but it seems like somehow CocoaPods is not properly setting up |
Digging around in the |
I added the line pod 'React-RCTPushNotification', :path => "#{prefix}/Libraries/PushNotificationIOS" to |
Upstream React Native still has it too, I guess they never got around to deleting it, and I kept the macOS support not realizing it was actually meant to be ejected. There has been active development in this code path by Meta, so interesting it is supposed to be ejected.. |
yeah it's just marked as deprecated for now. I'm trying to follow the react 0.71 docs on push notifications and adjust them to work with macOS instead of iOS based off of the So far after adding a couple of methods (⬇️) to my
|
So after more fiddling around I ended up in pretty much the same place I did on the issue I opened that I linked earlier in
the difference between the poking around the objective-C implementation of any advice or tips on why this might not be working or how one might debug this appreciated. edit: the additions i ended up making to my - (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification {
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
[RCTPushNotificationManager didReceiveUserNotification:notification];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
// Required for the remoteNotificationsRegistered event.
- (void)application:(__unused RCTUIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the registrationError event.
- (void)application:(NSApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(NSApplication *)application didReceiveLocalNotification:(NSUserNotification *)notification {
[RCTPushNotificationManager didReceiveUserNotification:notification];
}
// Required for the remoteNotificationReceived event.
- (void)application:(__unused RCTUIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
} I also tried to change @interface AppDelegate : RCTAppDelegate <UNUserNotificationCenterDelegate> |
I found something pretty confusing... if i look in this repo at the obj-c definition of this repo:RCT_EXPORT_METHOD(presentLocalNotification : (JS::NativePushNotificationManagerIOS::Notification &)notification)
{
NSDictionary<NSString *, id> *notificationDict = [RCTConvert NSDictionaryForNotification:notification];
UNNotificationContent *content = [RCTConvert UNNotificationContent:notificationDict];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1
repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[[NSUUID UUID] UUIDString]
content:content
trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:nil];
} local
|
@michelcrypt4d4mus what version of RNM are you using? The main branch probably matches what will come with 0.74, if not with what is already in 0.73 (you can check by looking at the same file on the branch |
|
Ah yeah.. those docs are out of date. Use the latest if you can, which would be 0.73. When that happens (with 0.75), we'd probably have to move the macOS implementation to the community module to keep support. |
@michelcrypt4d4mus I didn't thoroughly read through all of your posts here and this may not be the solution that you are looking for, but I solved this (and other native connections) through a different approach. I'm very bad at Objective-C, so I went with an EventEmitter that allows the JS and the Swift parts to talk to each other. Once you got that setup you don't need to hassle with getting the whole module into React, since you can do everything in Swift and it is a bit easier to find resources on that. Not a perfect solution, but a good one if you are coming from JS. You can take a look at my EventEmitter Swift class here. Also keep in mind that your notifications need a unique ID, otherwise MacOS will not deliver your notification if a notification with the same ID is already in your notification center. |
thanks. in my initial post i mentioned "The app can subscribe to and act on fake remote notifications triggered by react native's |
Thanks for the discussion here. I learned a bit about how people use React Native macOS, and a good place to put better macOS notification support :) |
ultimately i ended up firing my "real" notifications from a background process. |
Summary
I'm trying to figure out how to send notifications to the Notification Center in macOS similar to what Notifee or
PushNotificationsIOS
or expo do. Apologies if this is already a thing that one can do inreact-native-macos
but I've been poking around and haven't seen anything to this effect for macOS; is it something that is supported (or planned to be supported) in some way?I stumbled across this page about
IReactNotificationService
but I'm not really sure what to do with it - is this an objective-C interface?Motivation
Many (most?) native applications make use of the Notification Center for all kinds of things. Seems like it would be good for react native apps to be able to do the same.
Basic Example
No response
Open Questions
No response
The text was updated successfully, but these errors were encountered: