TL;DR How to use UNNotificationRequest with react-native?
Trying to implement push notification handling in react-native. In the react-native documentation it says that I should add the following to my AppDelegate.m:
// 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];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}
The problem is that the local notification method gives me a build error:
'UILocalNotification' is deprecated: first deprecated in iOS 10.0 - Use UserNotifications Framework's UNNotificationRequest
So that's easy to fix, I just need to use UNNotificationRequest, right? My question is, how do I import it?
The doc page looks like this:
https://developer.apple.com/documentation/usernotifications/unnotificationrequest
They assume that I know how to import it. Which I do not. How do I find out how to do this?
PS: No swift please. objc only. react-native uses objc.
Related
I have set up my app to work with GCM.
I have successfully added the code to integrate the GCM in my App.
Now I have two methods to handle the Push Notification:
Default Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
}
GCM Method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
}
Now I am confused where should I Handle my Notificaiton.
Where should I check application state and call my method to handle it.
Should I have to write method in both of these methods.
I'm not familiar with GCM but the two notification methods you listed standard UIApplicationDelegate methods and handle different scenarios.
application:didReceiveRemoteNotification: is called when the app is open and you receive a plain push notification. The types that you get alerted through notification center.
application:didReceiveRemoteNotification:fetchCompletionHandler: is called when the server is letting the app know there's something to download. You check the userInfo for what to download, initiate the download and call the handler(UIBackgroundFetchResult) upon NewData/NoData/Failed
Not sure what GCM does with these two methods but with that info you should be able to figure it out.
You should use GCM Method.
(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
}
In this method, You can handle your notification.
For example,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(#"Notification received: %#", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// [START_EXCLUDE]
if(application.applicationState == UIApplicationStateBackground){
//app is in background
}else if(application.applicationState == UIApplicationStateInactive){
//From background to foreground (user touchs notification)
}
handler(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]
}
My iPhone's screen is locked and sometimes, when I send push notifications a from PHP script, it disappears automatically. What can be the reason for this issue?
I use background fetch execution for didReceiveRemoteNotification:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {
NSLog(#"Notification received: %#", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
// 100 lines of code
}
How to receive the PhoneGap push notification in ios platform...?
I am using http://admin.pushapps.mobi console for sending a notification , I have configured everything for ios, and used the app token in my javascript file. I had sent a notification from the admin.pushapps.mobi, but it's not reviewing in my app.
I am using the demo given by them..... below is the full link https://github.com/PushAppsService/PhonegapBuildExampleApp
Can anyone please explain where I am wrong ? if there is any other push notification service having PhoneGap documentation, it would be helpful.
You should check 2 things for PushApps to work as expected:
Certificates and provisioning profile matching.
You should verify the your AppDelegate.m contains as mentioned in the wiki:
#import "PushApps.h"
#pragma mark - Push Notifications
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[PushAppsManager sharedInstance] didRegisterUserNotificationSettings:notificationSettings];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
[[PushAppsManager sharedInstance] handleActionWithIdentifier:identifier forRemoteNotification:userInfo
completionHandler:completionHandler];
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Notify PushApps of a successful registration.
[[PushAppsManager sharedInstance] updatePushToken:deviceToken];
}
// Gets called when a remote notification is received while app is in the foreground.
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo
{
[[PushAppsManager sharedInstance] handlePushMessageOnForeground:userInfo];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
// keeps you up to date with any errors during push setup.
[[PushAppsManager sharedInstance] updatePushError:error];
}
Don't use pushapps.mobi give you all limited notification and user plan and after you get more than on milion users then close your account and then tell you if you want return your account more and more money juswsh layer
I am trying to implement the silent push notification for my background fetch.
Now i am facing an issue while implementing the necessary code in my AppDelegate.
Before i had the following function in order to handle the push notification:
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Just Received a push!");
...
}
I added mow the below function to handle the background fetch silent notification:
//backgroundDownloadTask
- (void) application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Just Received a push!");
....
}
The weird thing is that when i receive a normal notification, it also entered the second function and the first one (old) is seems top be useless now, so I added the following to differenciate between the normal and silent one:
//backgroundDownloadTask
- (void) application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Just Received a push!");
NSDictionary *dict = [userInfo objectForKey:#"aps"];
if([dict objectForKey:#"content-available"])
{
//silent
}
else
{
//normal push
}
}
but the issue is when i am in the foreground and I receive a push, No function is executing as no push is received(Maybe because the second function is just for background).
So how am I able to handle both pushes? any idea ?
Thank you,
i implemented the Remote Notifications in my application! if my App is in Background and a Push Message was send to my Device, i react with this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}
This is working great when App is in Foreground or in Background State! But what if my App is not running at all?! CanĀ“t i react to Push Messages when the app is not running?I mean WhatsApp can do this, right?!
If user clicks on push notification from notification center you will have information in launchOptions with the push notification content and you can use below code to check if application was launched clicking push notification or it was there as well,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSLog(#"LaunchOptions->%#",launchOptions);
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self performNotificationAction:userInfo];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// NSLog(#"userInfo->%#",userInfo);
[self performNotificationAction:userInfo];
}
-(void)performNotificationAction:(NSDictionary*)userInfo{
//Do the stuf whatever you want.
//i.e. fetch the message or whatever extra information sent in push notification
}