How to manage notification when users click on badge - iphone

I have a question to ask about notifications. After some hours to learn how to implement push notifications on iPhone, it now arrives!
How do I manage users that click on badge or click view on alert? What happen when users click there?
I tried to send me some notification and the number on the icon of application in springboard increments. In which way clicking there it's possible to show a uiview to manage the notification arrived and show the message read and unread?
Is there a tutorial for it? I want to save all the messages inside a uitableview.

You want to read Handling Local and Remote Notifications
Basically in your application delegate, you want to implement:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
and
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
And process the launchOptions / userInfo for the notification data.
How I normally process the data is:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary* userInfo =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self processRemoteNotification:userInfo];
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self processRemoteNotification:userInfo];
}
The format for userInfo is documented the The Notification Payload section.
e.g. the "aps" key will give you another NSDictionary, then looking up the "alert" key will give you the alert message that was displayed. Also, any custom data you send in the JSON payload will be in there as well.
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alertMsg = #"";
NSString *badge = #"";
NSString *sound = #"";
NSString *custom = #"";
if( [apsInfo objectForKey:#"alert"] != NULL)
{
alertMsg = [apsInfo objectForKey:#"alert"];
}
if( [apsInfo objectForKey:#"badge"] != NULL)
{
badge = [apsInfo objectForKey:#"badge"];
}
if( [apsInfo objectForKey:#"sound"] != NULL)
{
sound = [apsInfo objectForKey:#"sound"];
}
if( [userInfo objectForKey:#"Custom"] != NULL)
{
custom = [userInfo objectForKey:#"Custom"];
}

Related

AppleWatch : On button tap from the the AppleWach Open URL on iPhone device

I want to open url from the AppleWatch app button tap to the mobile application.
Please any one have any idea regarding to these?
Added this in your AppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
This is what you have to apply on button tap in Watch App
NSURL *url = // your URL
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:url, #"URLKey", nil];
[WKInterfaceController openParentApplication:dic reply:^(NSDictionary *replyInfo, NSError *error)
{
NSLog(#"%# %#",replyInfo, error);
}];
And you can receive this request in iPhone in AppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
//receive userinfo dictionary here to perform specific request from watch
NSString *url = [userInfo objectForKey:#"URLKey"];
// perform operation on url
reply(#{#"Notification Alert":[NSString stringWithFormat:#"%f",dist]});
}

AppDelegate is not listening to APNS Push Notification

When my app enters background, AppDelegate is not listening to the new push notification from APNS. But strange thing is that it doesn't happen all the time. Sometimes it works, sometimes it does not. I can't figure out why.
Here is my sample code for listening APNS Notification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
}
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
}
}
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Received notification background: %#", userInfo);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
// Check result of your operation and call completion block with the result
completionHandler(UIBackgroundFetchResultNewData);
});
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
In background/closed mode,no code will execute and none of the delegate method will be invoked.This time push notification is handled by OS itself.The app badge is set by push notification payload value coming from server side.
If you are not getting app badge in background,it is a badge number issue from server side.Check whether the push notification payload contains application badge field and set to values greater than 0.
This link have same issue and resolved it.
Try to change your line..
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}
in iOS 8 you have to use "UIUserNotificationTypeBadge"...
Hope this will work for you..

App with playerduel functionality doesn't get push notification

I am developing application which support playerduel framework. In which two players can play with each other. Person can send challenge to another one.
i follow the documentation following. https://docs.urbanairship.com/display/DOCS/Getting+Started%3A+iOS%3A+Push
I can get notification when i send it from command line (for testing) as describe in above documentation. But when i play game. Playerdual can't send notification when some one send challenge to another.
appdelegate code :-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Init Airship launch options
NSMutableDictionary *takeOffOptions = [[[NSMutableDictionary alloc] init] autorelease];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Create Airship singleton that's used to talk to Urban Airship servers.
// Please populate AirshipConfig.plist with your info from http://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
// Register for notifications
[[UAPush shared]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Override point for customization after application launch.
self.appStarted = YES;
UIImage *bgImage= [UIImage imageNamed:#"default.png"];
[PlayerDuel initializeWithGameKey:#"gamekey" andBackground:bgImage
andDelegate:[navigationController.viewControllers objectAtIndex:0] andOrientation:UIInterfaceOrientationPortrait];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"deviceToken:- %#",deviceToken);
// Updates the device token and registers the token with UA
[[UAPush shared] registerDeviceToken:deviceToken];
[PlayerDuel registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
}
NSLog(#"remote notification: %#",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alert = [apsInfo objectForKey:#"alert"];
NSLog(#"Received Push Alert: %#", alert);
NSString *sound = [apsInfo objectForKey:#"sound"];
NSLog(#"Received Push Sound: %#", sound);
NSString *itemName = #"my app";
NSString *messageTitle = [apsInfo objectForKey:#"alert"];
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive){
AudioServicesPlaySystemSound(1007);
[self _showAlert:messageTitle withTitle:itemName];
}
else{
UIViewController *viewController = navigationController.visibleViewController;
// NSLog(#"Controller Name:- %#",viewController);
[viewController.view reloadInputViews];
[viewController playerDuelStartGame:nil];
}
NSString *badge = [apsInfo objectForKey:#"badge"];
NSLog(#"Received Push Badge: %#", badge);
}
If the push notifications work directly through Urban Airship and not through PlayerDuel, You probably didn't specify the right urban airship details in PlayerDuel's developers website. Make sure you put Urban Airship's Master Secret and not the App Secret in PlayerDuel's website.
I have used App Secrete as Urban Airship Key: . This is wrong. When i change it value as App Key . It works fine.

how to handle push payload when the app closed?

i am sending push payload to my users with the follow content :
{"aps": {"alert": "Go To Google", "sound": "Default","url":"http://www.google.com"}}
everything goes well when the pp is running but in the background.
if i am receiving the push and the app is closed i am open it and nothing happen.
i am trying to redirect to this url in the payload.
again when the app is running from the background it goes well.
this is the implementation so far AppDelegate.m:
-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
RedirectedUri = [[userInfo objectForKey:#"aps"] objectForKey:#"url"];
NSLog(#"%#",RedirectedUri);
[self Redirect:RedirectedUri];
}
need some help please.
Additionally, add the following to your code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self applicationDidFinishLaunching:application];
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
RedirectedUri = [[dictionary objectForKey:#"aps"] objectForKey:#"url"];
[self Redirect:RedirectedUri];
}
}
return YES;
}

Showing Local Notifications using Location Manager, While app is in the background

I am new to iPhone Programming .I have developed one application for checking user entered into a particular region.But i need to check in Background.In background i am checking but the problem is repeating the UILocalNotification alerts .
So how to prevent the repeated UILocalNotifications
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"running in background ...");
[self checkRegionEntered];
CurrentlattitudeValue1 =newLocation.coordinate.latitude;
CurrentlongitudeValue1=newLocation.coordinate.longitude;
}
-(void)checkRegionEntered
{
if ([testRegion containsCoordinate:currentCoordinates])
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
UILocalNotification *notif = [[cls alloc] init];
NSDate *now = [NSDate date];
[notif setFireDate:now];
if([Obj.NotesGeo length])
[notif setAlertBody:Obj.NotesGeo];
else
{
[notif setAlertBody:[NSString stringWithFormat:#", you have arrived at %#",Obj.NameGeo]];
}
[notif setAlertAction:#"Launch"];
notif.soundName=[NSString stringWithFormat:#"%#.wav",Obj.Ringtone1];//[NSString stringWithFormat:#"%g",Obj.LatitudeGeo]
NSDictionary *userDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"%f",Obj.LatitudeGeo] forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
}
}
}
This may help. This logic is designed to ensure that your notification will only fire once, and that the same alert is displayed regardless of whether the app is in the foreground or background when it fires.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
//detect if app was launched by notification
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
//trigger our custom notification handler
[self handleLocalNotification:notification];
}
return YES;
}
- (void)handleLocalNotification:(UILocalNotification *)notification
{
//this is our custom method to handle the in-app notification behaviour
[[[[UIAlertView alloc] initWithTitle:#"You have a notification"
message:#"Yay!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//this standard application delegate method is called under the following circumstances
//1. the app is running in the foreground when the notification fires
//2. the app was running in the background when the notification fired, and the user pressed the action button
//confusingly, if the app was not running when the notification fired, this method won't be called at startup
//automatically and you need to check for the notification key in the launch options instead
if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground)
{
//this code emulates the same behaviour as when a local notification is received
//when the app is not running (except for playing the sound)
//store notification temporarily
[lastNotification release];
lastNotification = [notification retain];
//get button labels
NSString *actionButton = nil;
NSString *cancelButton = #"Close";
if (notification.hasAction)
{
actionButton = (notification.alertAction)? notification.alertAction: #"View"; //defaults to "View" if nil
cancelButton = #"OK";
}
//show alert
[[[[UIAlertView alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleDisplayName"] //name of application
message:notification.alertBody
delegate:self
cancelButtonTitle:cancelButton
otherButtonTitles:actionButton, nil] autorelease] show];
}
else
{
//trigger our custom notification handler
[self handleLocalNotification:notification];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
//trigger our custom notification handler
[self handleLocalNotification:lastNotification];
}
}