Closed googlecomvn closed 5 years ago
Same here, local notification not showing. I am testing in an iPhone X, iOS 11. The event notificationReceivedForeground does fire though.
Can confirm this as well. Thought it was my own fault at first causing a race condition since I remove old notifications before pushing a new one.
I really don't know how to show local notification, document is so difficult to understand and follow
I managed to make localNotifications show up, by using UNUserNotificationCenter. Here is what I added:
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
Just below the #imports:
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
In didFinishLaunchingWithOptions:
if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if (!error)
{
[[UIApplication sharedApplication] registerForRemoteNotifications]; // required to get the app to do anything at all about push notifications
NSLog( @"Push registration success." );
}
else
{
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
}
}];
}
Added methods:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
[RNNotifications didReceiveRemoteNotification: formatUNNotification(notification)];
completionHandler(UNNotificationPresentationOptionAlert);
}
// When notification is clicked
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
UNNotification *notification = response.notification;
[RNNotifications didNotificationOpen: formatUNNotification(notification)];
completionHandler();
}
static NSDictionary *formatUNNotification(UNNotification *notification)
{
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
UNNotificationContent *content = notification.request.content;
formattedNotification[@"identifier"] = notification.request.identifier;
if (notification.date) {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
NSString *dateString = [formatter stringFromDate:notification.date];
formattedNotification[@"fireDate"] = dateString;
}
formattedNotification[@"alertTitle"] = RCTNullIfNil(content.title);
formattedNotification[@"alertBody"] = RCTNullIfNil(content.body);
formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier);
formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier);
formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
return formattedNotification;
}
In Project > Libraries > RNNotifications.xcodeproj > RNNotifications.h, add:
+ (void)didNotificationOpen:(NSDictionary *)notification;
Hi Axlalvaro Does your tips allow to show notification on the mode foreground?
@googlecomvn yes!
Hi Axlalvaro Thanks, I will try
Hi Axlalvaro Has error with this following line in AppDelegate.m
[RNNotifications didReceiveLocalNotification:localNotification];
In Xcode 9.2 has error: Unknown receiver 'RNNotification'; did you mean 'UNNotification'?
@googlecomvn Did you put the required methods in your AppDelegate.m?, as indicated here:
https://github.com/wix/react-native-notifications/blob/master/docs/installation.md
You have to add the methods specified in the link in the iOS section and import:
#import "RNNotifications.h"
I just copied a piece of my AppDelegate.m, not the whole.
Hi axlalvaro The Notification is now working. But when the app is suspended or closed, the notification is not working. Do you have any suggestion to this scenario
I followed the instruction in link https://facebook.github.io/react-native/docs/linking-libraries-ios.html but in AppDelegate.m there is an error: RNNotifications.h' file not found, although I have the following import in this file
It would be really awesome to get an update on this, because it seems like the latest build is broken. Is there a way to merge the code @axlalvaro wrote into the project?
After searching around I have a simple solution at last (I tested on my iphone 7) : add this following method into AppDelegate.m
@axlalvaro Your solution isn't 100% working though, they do show but when I click on push notification it doesn't trigger onNotificationOpened.
@ItsNoHax You are right! I had more code in my project. I just edited my code above. Let me know if it works (it does for me)!
Will let you know tomorrow morning! Very much appreciated!
On Wed, 19 Dec 2018 at 17:38, axlalvaro notifications@github.com wrote:
@ItsNoHax https://github.com/ItsNoHax You are right! I had more code in my project. I just edited my code above. Let me know if it works (it does for me)!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wix/react-native-notifications/issues/203#issuecomment-448660891, or mute the thread https://github.com/notifications/unsubscribe-auth/AEBxyt-S_Lnexbtc5JZUjKQzgpLoatIXks5u6mttgaJpZM4THzB- .
@axlalvaro I'm getting 'No known class method for selector 'didNotificationOpen:'
It's because the didNotificationOpen is not registered in the RNNotifications interface.
How did you work around it?
@ItsNoHax Umm, I can't remember what I did, it was a long time ago, but reading the RNNNotifications.m, it does have the didNotificationOpen method. So I guess I just added it to the RNNotifications.h file. I updated my comment above with this line. Hope it finally works!
@axlalvaro But that means every time you clean/reinstall your node_modules that change will disappear? :/
@ItsNoHax Yes, sad moment. I just can't think of any other way of achieving this in a fast way (no PRs), because the method didNotificationOpen is not exposed in the .h :(
@axlalvaro So with your changes the push notifications are showing but when I press them they are not triggering in the notificationOpened listeners :/
@ItsNoHax Does it stop at didReceiveNotificationResponse in AppDelegate.m if you set a breakpoint?
So I managed to make it work partly through your solution, I used https://github.com/ds300/patch-package to make the function available in the header file (without having to make a fork or keep changes locally).
Also for the following method didReceiveNotificationResponse I had to make sure I pass the userInfo object of the formatted message to didNotificationOpen. If you pass the whole formatted message the listener get's a wrongly formatted notification.
@axlalvaro
I have the same issue. Local notifications doesn't work for me...
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.
The issue has been closed for inactivity.
After searching around I have a simple solution at last (I tested on my iphone 7) : add this following method into AppDelegate.m
- (void)userNotificationCenter:(UNUserNotificationCenter )center willPresentNotification:(UNNotification )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { NSLog( @"Handle push from foreground" ); // custom code to handle push while app is in the foreground completionHandler(UNNotificationPresentationOptionAlert); }
I tried this one but notification not received in the foreground. Do I need to install PushNotificationIOS to work? Background notifications are fine. Running in IOS 13.3
I have follow the instruction to install the lib in Xcode 9. I use local notification for my app and run in simulator iPhone 6 and run in real device iPhone 7 but the notification is not visible. Please help me My snippet code to show notification
_let localNotification = NotificationsIOS.localNotification({ alertBody: "Local notificiation!", alertTitle: "Local Notification Title",