I am trying to develop an iOS app using UrbanAirship. I receive push
notifications but cannot get any delegate to be called when the app is in
the background. I (wrongfully it seems) assumed that the
handleBackgroundNotification API from the UAPushNotificationDelegate
class would provide me the functionality to execute custom actions when
receiving the notification when app is in the background.
This is what their documentation says:
"handleBackgroundNotification:
Called when a push notification is received when the application is in the background
- (void)handleBackgroundNotification:(NSDictionary *)notification
Parameters
notification
the push notification
"
- see https://docs.urbanairship.com/ios-lib/Classes/UAPushNotificationHandler.html#//api/name/handleBackgroundNotification:
Doesn't seem to work that way - sure seems the OS is keeping the notification
for itself - which is inline with Apple's documentation.
I am questioning the purpose of the function if the OS doesn't allow it. I use didReceiveRemoteNotification for receiving remote push notifications which works just fine!
However, since this is an enterprise application (i.e. not App Store),
if there are private APÏs and frameworks that would allow me to do this,
I would appreciate any assistance. There is no way this app would ever
make it to the app store!
The custom actions I am trying to execute include, for example, a
notification receipt sent to a server that would "prove" the recipient
app did indeed receive the notification, play a custom sound at maximum
volume (bypassing silence and do not disturb mode). These are some
requirements from the client.
This is what I use and its working just fine:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if(!loggedIn) return;
NSLog(#"WOW: got notification! %#", userInfo);
// see this for fine tuning: http://fivelakesstudio.blogspot.com/2012/04/push-notifications-and-urban-airship.html
[[UAPush shared] handleNotification:userInfo applicationState:application.applicationState];
[[UAPush shared] resetBadge];
sharedApplication.applicationIconBadgeNumber = 0; // probably redundant
[self handlePushNotification:userInfo isBooting:NO]; // my common handler
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UA_LINFO(#"Application received remote notification: %#", userInfo);
[[UAPush shared] appReceivedRemoteNotification:userInfo applicationState:application.applicationState fetchCompletionHandler:completionHandler];
NSDictionary *values = [userInfo objectForKey:#"aps"];
NSString *title = [values objectForKey:#"alert"];
}
process the notification received for all states in this block.
NB. you can monitor the application.applicationState value to note the state of the app when the notification is received.
Hope this helps
Related
I have this problem now, I need to handle the info of push notification when app in backround, I mean, even not tap the notifcation.
That mean this application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler must be called in background !
Here is my code for it
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"full message %#", userInfo);
NSDictionary *aps = [userInfo objectForKey:#"aps"];
NSLog(#"full aps %#", aps);
NSString *custom = [userInfo objectForKey:#"custom"];
NSLog(#"full custom %#",custom);
completionHandler(UIBackgroundFetchResultNewData);
[[NSNotificationCenter defaultCenter] postNotificationName:#"notificationRemote" object:nil userInfo:userInfo];
}
I really need handle the info, such that save it to local everytime I have any notification, no matter I tap in push notification.
I search and I saw that I need content-available for my payload, but I can't find it in Firebase Console Notification.
Yup, you need content-available key in payload for silent push notification.
for more about FCM, U can see this link:
Firebase silent apns notification
You can use FireBase Cloud Function to create a payload and Fire a Notification.You can handle events in the Firebase Realtime Database.
The Realtime Database supports the onWrite() event, which triggers anytime data is created, destroyed, or changed in a specified database location.
Please refer this tutorial for detail which may help you
https://code.tutsplus.com/tutorials/serverless-apps-with-firebase-cloud-functions--cms-28557
if I understand correctly, the UIApplicationLaunchOptionsRemoteNotificationKey key is used on the -[UIApplicationDelegate application:didFinishLaunchingWithOptions:] method when
- the push was received when the application was not running (e.g. killed)
- the user clicked on the received push
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo) {
// app was not running and the user clicked on the push
}
}
but .. in this exact same case, the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is also called just after the previous one.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
// called when
// app was not running and the user clicked on the push
// app was running in background and user clicked on a push
// app was running in background and a silent push was received
// app is in foreground and a push is received
completionHandler(UIBackgroundFetchResultNewData);
}
So the question is, why should I use the UIApplicationLaunchOptionsRemoteNotificationKey if everything can be handled in the application:didReceiveRemoteNotification:fetchCompletionHandler delegate? Did I miss something?
cheers,
Jan
In case when the app is killed and user taps on push notification in notification center, launchingOptions dictionary contains UIApplicationLaunchOptionsRemoteNotificationKey so that you can adjust your app start logic.
In prior iOS version there wasn't application:didReceiveRemoteNotification: fetchCompletionHandler: and launchingOptions dictionary from application:didFinishLaunchingWithOptions: was the only place where you could handle remote notification on app start.
My guess is that application:didFinishLaunchingWithOptions: contains UIApplicationLaunchOptionsRemoteNotificationKey for compatibility reasons.
The presence of this key indicates that a remote notification is available for the app to process. The value of this key is an NSDictionary containing the payload of the remote notification.
i have a question. I have configured my app for development push notifications and all was working fine.
All push notifications arrived the app on the device. But i want to handle the push notification in a special way. If my app is running or in background the function
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
will work fine. But is the app killed the function doesn't work. I need a idea to handle incomming push notifications whether if the app is running, in background or killed. If a push was received the app must call a callback URL. if there any way to realize this?
for example i send the following apn string:
{"aps":{"content-available":1,"alert":"This","badge":1},"callback":"https://www.xxxxxxxxx.com/sf/daniel_push/985270815/12323453534534/?device(id)=1111111111&s="}
At the end all what i want is to call the callback url if the app becomes a push notification.
Try this in didFinishLaunching:
NSDictionary *userData = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userData) {
// handle notification
}
Hi I am developing small IOS application in which I am using push notification. So in my case I am able get notifications and I am also able to access data for notification. Data from notification mean title, description etc. So in my case I am not saving my notification at my server side. I want to save those locally. For that what I want as soon as notification come I want to save that data locally. I am able to access my data when App is in foreground but I am not able to access my notification data when app is in background. I want to access the data of notification so that I can save it. For notification I did following things:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (NSString *key in [userInfo allKeys])
{
NSString *data = [userInfo objectForKey:key];
NSLog(#"inside did register for notification .... %# ---- > %#",key,data);
}
}
My requirement is simple I want access to notification when my app is in background. Any one is here who already did this? Need Help. Thank you.
In iOS, the app cannot access it's push notification until the user taps on the push notification from the notification center.
Once the push notification is tapped and the app loads/becomes active, only then will you be able to access the push notification.
FYI:
When the app is in background and a push notification is recieved.
After the user taps on the push notification:
the contents will be accessible in the -didReceiveRemoteNotification: method.
When the app is not open and a push notification is received.
After the user taps on the push notification
the contents will be accessible in the -didFinishLaunchingWithOptions: method in it's launchOptions parameter.
Also... push notifications aren't 100% reliable. They may or may not be delivered (although them not being delivered is pretty rare in my observation but worth pointing out none-the-less)
There is a way in iOS 7.0 and later where you can send notifications and access its content without user tapping it.
So, the notification payload which you send has a parameter called content-available. You can view the payload parmeters in the following link:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
So when you set the content available to 1 and deliver the notification, iOS calls the below function even if the app is not in the background or foreground(must have remote notification enabled)
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Then you can use the userInfo dictionary to fetch your notification data.
I have tried silent Apple push notification in ios 7 using following code. The Push notification received when application is in foreground or in background. Then I remove application from background by swiping the application from background apps. After that If I send a Push notification from my server, it sent to APN properly, but not delivered to iPhone.So Apple push notification in ios 7 delivers silent push notification only application running in backgroud? If user remove application from background will it receive notification or not?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSString *receivedMessage = [[userInfo objectForKey:#"acme1"] objectForKey:#"mydata"];
NSLog(#"fetchCompletionHandler receivedMessage -> %#",receivedMessage);
completionHandler(UIBackgroundFetchResultNewData);
}
didReceiveRemoteNotification not called when app will not run,it only called when app will open and foreground.
you will get list of notifications in UILocalNotification,if app not run and it receive notifications like this :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.........
.........
.........
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSLog(#"****** notifiation ******");
}
return YES;
}
App will be able to receive the notification even when app is closed i.e. not in background mode.