Comments (15)
// fetch notifications in the background and foreground
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"Notification Body %@", notification);
}
based on http://stackoverflow.com/a/36750318/2628278
In order for notifications to hit your app in the background you need to also define a fetchCompletionHandler, with a completion handler function like below. The aps:{content-available:1} payload should wake up you application and trigger this code in your AppDelegate, and in turn hit your JavaScript in RN.
from react-native-push-notification.
related to #17.
Have a look at my last comment in the thread. Also, don't forget to add remote-notification
to your list of background modes
from react-native-push-notification.
Is there a way to give my RN app a few seconds to deal with the push notification? It needs to download some data basically and it looks like it's not been given enough time to do it.
from react-native-push-notification.
@npomfret Do you mean some time before the remote notification is shown or some time for it to complete the remote fetch task?
from react-native-push-notification.
It just needs time to do some work
from react-native-push-notification.
I think it is possible in iOS by enabling remote fetch for the notifications for which you want to do some work. You will need to set content-available
to 1 for such notifications, enable background fetch mode in the capabilities and also provide didReceiveRemoteNotification
method in the AppDelegate
file. If you search for remote fetch in iOS, I think you will find ample information on what needs to be done. It requires use to call the completion handler in the didReceiveRemoteNotification
method when the work is done and since it is a bit tricky to know when the work is done, what you can do is emit an event from the native side which will be caught by the JS side to perform the work and just sleep for a few seconds and then call the completion handler. The maximum amount of time you are given to complete the remote fetch work is 30 seconds so you have to call the completion handler before that time. It is a bit hacky but just wanted to share how I am solving it. For Android, it is a bit tricky. Android always wakes up your app when a remote notification is received if it is running in the background and you can again send an event from the native Java side which will be caught by the JS side to do some work but it doesn't provide any sort of completion handler that you have to call to sleep the app again and does it on its own. In my experience, it has been able to finish up some websocket requests in the background.
from react-native-push-notification.
I've done all that stuff you've mentioned. The problem seems to be that the app gets suspended after a while and then killed by the OS. At this point if a push notification arrives (with content-available=1) it will try to launch the app, which it does. But it doesn't seem to give it long enough to get going. My app takes a few seconds to start, but i seems like the OS is only giving it a very short time to start. To make things more complicated I don't know how to get my app to tell my app delegate that it's finished processing the push notification and that it should call the completion handler. For the moment I'm just trying a [NSThread sleep...]
in order to give the RN app a few seconds to do some work...
Is there a more sensible way to do this? How do I get my RN app to tell me app delegate that it has processed the push notification?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"notification-body => %@", notification);
if(application.applicationState == UIApplicationStateInactive) {
NSLog(@"Inactive");
} else if (application.applicationState == UIApplicationStateBackground) {
NSLog(@"Background");
} else {
NSLog(@"Active");
}
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
if(application.applicationState != UIApplicationStateInactive && application.applicationState == UIApplicationStateBackground) {
self.bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@ starting background task %lu", self, (unsigned long)self.bgTask);
[NSThread sleepForTimeInterval:15.0f];
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
NSLog(@"%@ ended background task %lu", self, (unsigned long)self.bgTask);
completionHandler(UIBackgroundFetchResultNewData);
});
} else {
completionHandler(UIBackgroundFetchResultNoData);
}
}
from react-native-push-notification.
The answer to this will be in react-native 0.38 I think
from react-native-push-notification.
Is it possible to display a local notification while the app is in the background?
from react-native-push-notification.
Yes its possible to display a local notification while the app is in the background.
from react-native-push-notification.
@hyperh do you still have this issue? if so, have you tried the latest release? if not, can we close it?
from react-native-push-notification.
@npomfret I am on RN 0.39.0, however, the app does not wake up if app closed by the user.
On remote notification, this is what I see in the logs of the device.
Notification received
Received message for enabled topic '<app bundle identifier>' with payload '{
aps = {
alert = {
body = "<null>";
title = "Test Heading";
};
"content-available" = 1;
};
custom = {
a = {
content = "This is a test Content";
subTitle = "Test SubTitle123";
title = "Test Title123";
type = alert;
};
i = "e9bf6a4a-94ef-4d9e-95d8-80870d0db905";
};
}' onInterface: NonCellular for device token: NO with priority 10
The next logs are
High Priority Push: <app bundle identifier> - App killed
Ignoring notification with no content (<app bundle identifier>): BDD8-38AC
The AppDelegate.m has the below code:
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Optionally implement this method over the previous to receive remote notifications. However
// implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one whenever possible.
// If your delegate implements both methods, the app object calls the `application:didReceiveRemoteNotification:fetchCompletionHandler:` method
// Either this method or `application:didReceiveRemoteNotification:fetchCompletionHandler:` is required in order to receive remote notifications.
//
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the notification event.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
NSLog(@"push-notification received: %@", notification);
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}
I am not sure if there is something to do with the completionHandler but I have also tried
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
completionHandler(UIBackgroundFetchResultNewData);
});
}
without any success!
What I am trying to do is receive a silent remote notification, do my stuff and push a local notification.
The app seems to receive the silent notification, however, the RN thread does not get called.
Not a problem with this package, but something to do with the correct configuration of the PushNotifications
Update
I do not receive Remote silent notification when the device is locked or inactive (screen off), but if I wake the device and unlock, I see the messages received in the console
app.
Guess, I will have to live with remote noisy notification for now.
P.S. I am using one signal for remote notifications and have also installed react-native-onesignal package.
Here is the gist of how the Notifications are configured within the app
from react-native-push-notification.
Yes that's expected behaviour and described in the trouble shooting guide. On iOS the app os will never relaunch the app if the user killed it.
from react-native-push-notification.
Can we close this issue?
from react-native-push-notification.
@npomfret I stopped working on the project which used this library. Feel free to close it if you like.
from react-native-push-notification.
Related Issues (20)
- wrong sound when update version of app
- Foreground onNotification behaviour on iOS HOT 2
- Why are people still using this outdated library? HOT 3
- Indirect notification activity start (trampoline) from com.xxx blocked HOT 1
- How to pass and get data when notification clicked from .onNotification()
- -
- delete
- update build gradle version
- Scheduled Notifications are not displaying..... HOT 1
- Schedule Daily Notification is not working after few days if i did not open the app
- Android - Push notifications not received when app is closed (Killed/Terminated/etc) HOT 5
- Get onNotification triggered when a remote notification is received HOT 1
- Is there a method to persistently display the badge count, even after all notifications have been cleared or dismissed?
- Asking notification permission for Android 13 and above in react native version 0.61 HOT 1
- How can I ask notification permission for Android 13 and above in react native version 0.61 using POST_NOTIFICATIONS? HOT 1
- Default FirebaseApp is not initialized in this process com.client. Make sure to call FirebaseApp.initializeApp(Context) first. HOT 1
- play the notification sound simultaneously on android and bluetooth device
- Hello, I would like to call a function when the use click to a notification I have looked in the other issues to answer this question. And I have seen many responses to this subject. I need to use PushNotification.configure as well as onNotification: function The problem and when I use this function I get the following error: Default FirebaseApp is not initialized in this process I only use local notifications so I don't have to install firebase. Is there a way to call a function when the user clicks on a notification without using firebase? Thank you HOT 1
- onRegister is never called on aab, on apk its working fine HOT 1
- What Callback to run any statement when notification is show in iOS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-push-notification.