How to take the text string from the push notifications? - iphone

I have an Iphone application in which i am recieving push notifications from the server.Now i am going to a view controller to show the message.Where that same message is loaded in a tableview .so thats not a problem.Now i am recieving two kinds of messages,one is a link and another is the message as earlier.if it is a link i want to open it in saffari,not need to go to tableview as usual.Can anybody help me to achieve this?

When You click on the push notification then you get a dictionary in the function - didReceiveRemoteNotification:
try this code:-
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"remote notification: %#",[userInfo description]);
if (userInfo)
{
if ([[userInfo allKeys] containsObject:#"aps"])
{
if([[[userInfo objectForKey:#"aps"] allKeys] containsObject:#"alert"])
{
if([[[userInfo objectForKey:#"aps"] allKeys] containsObject:#"alert"])
{
NSString *urlString = [[userInfo objectForKey:#"aps"] objectForKey:#"alert"];
NSURL *url = [NSURL URLWithString:urlString];
if(url)
{
[[UIApplication sharedApplication]openURL:url]; // open in the safari...
}
else
{
// use the message in table view
}
}
}
}
}
}

The push notification is tied to your application, so in short its not possible to have a push notification from you application open safari directly. However, one workaround might go something like this:
user responds to notification (ie swipes to open it on the lock screen)
your app opens, and the data from the notification is passed into your applications
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method. You could then interrogate the the data passed in as so
NSDictionary *dictionary = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
// If dictionary is not nil, then your app is launched due to a push notification
if (dictionary != nil) {
NSDictionary *payload = [tmpDic objectForKey:#"aps"];
}
Once you've got your payload, look at the contents, and if it is a URL, call the safari URL scheme, passing in the URL. Like so
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: <URL from payload>]];
This will help you achieve what you want to do, but it may mean the user briefly sees your app first before the os will switch them into safari.
A side note, why would you want to do this? Your users should not be launching a random URL from a notification, and I don't think Apple would like that too much. Prehaps they should be seeing some information about the URL first in your app, and then choosing whether they would like to open it in safari?

Related

Turn Push Notifications On and Off from within App with Parse

I have been advised that I should allow users to turn push notifications on and off from within my app. I am using Parse to manage my push notifications. I have everything setup so that a user can register for notifications by pressing "Allow." when the push alert pops up. My question though, is about how I would permit the user to turn push notifications ON from within the app if they had originally said "Don't Allow." I know that the push notification alert will only display once. Does anyone have any ideas? Thanks!
My App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
[Parse setApplicationId:#"APP_ID"
clientKey:#"CLIENT_KEY"];
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
//other code
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
User Settings View Controller:
-(IBAction) switchValueChanged {
if (toggleSwitch.on) {
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
else {
}
}
You cannot do that. The user has to manually go into the Notification Settings and set your app for notifications. Apparently the reason being is that Apple does not want an app pestering a user to allow if the user has already said no once.
I suggest having your app display an alert to advise the user of turning notifications on.

User taps on UILocalNotification: Can it pass data to the app?

I am creating a local UILocalNotification and displaying it to the user as a banner. Is it possible to set it up so that when the user taps it and returns to the app, the app will receive some kind of data on the specific kind of notification it was? I want to open a specific view controller in the app. I think the best way would be to essentially send a URL to the app, or is there a way to get access to the UILocalNotification so that I can test which kind is was and do the right action?
To get data from the local NSUserNotification that is passed to an iOS app, all that you need to do is implement the following method: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification.
The problem is, this is called both when a local notification is posted when the app is in the background (i.e. when the user taps on the notification and then returns to the app), and also if the app is in the foreground at the time when the local notification fires (it is on a timer, after all). So how should one figure out if the notification was fired when the app was in the background or foreground? It's pretty simple. Here's my code:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
} else {
// App was running in the foreground. Perhaps
// show a UIAlertView to ask them what they want to do?
}
}
At this point you can handle the notification based on notification.userInfo, which holds a NSDictionary.
ust implement the NSUserNotificationCenterDelegate and define this method:
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
Example:
This is what I did in a "notifier" application.
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSRunAlertPanel([notification title], [notification informativeText], #"Ok", nil, nil);
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
notifications=nil;
[tableView reloadData];
[center removeDeliveredNotification: notification];
}
When the notification is activated (click by the user) I just inform the user with a panel (I could use a hud window).In this case I immediately remove the delivered notification, but this is not what happens usually.The notification could stay there some time and be removed after 1/2 hours (it depends on the application that you are developing).
1 - Define some class in your project to implement NSUserNoficationCenterDelegate protocol (documented here)
#interface someObject : NSObject <NSUserNotificationCenterDelegate>
{
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification;
}
2 - Set an instance of the object defined #1 as the delegate of default notification center.
[[NSUserNotificationCenter defaultNotificationCenter] setDelegate: someObject];
Now you will get called on didActivateNotification any time the user taps/clicks on the notification. You will have the original notification you created. So any information that you need should be available to you.
If you want/need special information (other than notification title, message, etc) you will probably need to set additional application specific information in your notification before you schedule it to be sent. For example:
NSUserNotification* notification = [[NSUserNotification alloc] init];
NSDictionary* specialInformation = [NSDictionary dictionaryWithObjectsAndKeys: #"specialValue", #"specialKey", nil];
[notification setUserInfo:specialInformation];

How to create a custom alarm which can be set for random days in a week without using local notifications?

I have studied some of the following questions, that are::
1.) How to set alarm for selected days in iphone?
2.) iPhone alarm using repeated local notifications
3.) How to set the alarm in iPhone and save in local notification?
but they all are using local notifications. I am facing problem with local notification as I have to use three buttons on an Alarm View which are: Snooze, Ignore and Okay. I have to perform custom actions on each button click.
Please help me with this stuff.
Suggestions accepted.
Thanks in advance.
Kuldeep.
In you app delegate...
- (void) presentWidget: (NSString*)theDisplayedText {
BOOL widgetIspresent = [WidgetVC widgetIsCurrentlyPresented];
if (!widgetIspresent) {
WidgetVC *widgetVC = [[WidgetVC alloc] initWithNibName:#"WidgetVC" userInfoString:theDisplayedText bundle:nil];
widgetVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
widgetVC.userInfoStr = theDisplayedText;
[mainScreenManager presentViewController:widgetVC animated:YES completion:nil];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification didFinishLaunchingWithOptions %#",localNotif);
NSString *theDisplaytext = [localNotif.userInfo valueForKey:#"someKey"];
//here we have to handle whatever notification was pressed - that might also be an old aready passed one
//this all is checked when the widget opens - it will show if the notification is OK or too old
[self presentWidget: theDisplaytext ];
} else {
//so we started the app normally, not via local notification
[self presentWidget];
}
return YES;
}
// Handle the notificaton when the app is running
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotif {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification didReceiveLocalNotification %#",localNotif);
NSString *theDisplaytext = [localNotif.userInfo valueForKey:#"someKey"];
[self presentWidget: theDisplaytext];
}
You need a way to start the UIViewController for the widget, I just created a mainScreenManager helper.
So on the widgetVC you have your "Snooze" and "OK" while the "Ignore" is just the ignoring of the notification itself.
Hope this gets you on an usable track...
ps I ported my app from Android to iPhone, that's why I used the name "widgetVC" for this screen. On Android it is implemented as a widget. Hope no iPhone lover feels offended :-)

How to change application badge with push when application is closed

I'm trying to change the badge of the application I'm developing. When closed, unfortunately I was only notified a message without changing the badge.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];
NSDictionary* userInfo = [launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSInteger badge = [[apsInfo objectForKey:#"badge"] integerValue];
if( [apsInfo objectForKey:#"alert"] != NULL)
{
application.applicationIconBadgeNumber = badge;
}
But that does not change anything. I have not found any solution around. What method is invoked on arrival of a push notification and the application is closed? I also added:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
NSLog(#"remote notification: %#",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alert = [apsInfo objectForKey:#"alert"];
NSLog(#"Received Push Alert: %#", alert);
NSString *badge = [apsInfo objectForKey:#"badge"];
NSLog(#"Received Push Badge: %#", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:#"badge"] integerValue];
#endif
}
But nothing...
Can you help me?
The badge icon number is governed by the badge property in the payload received as push message. The app itself has no role to play in what number shows up till the app becomes active. Setting the 'badge' property to any integer value (Note : it should not be string and should NOT be enclosed within "") does what is needed. The OS on receiving the notification looks for the value for 'badge' and immediately sets it on the app badge icon. All this happens when app is not active. To make sure it increments or decrements based on what happens in the app, the app should send message to server with the updated badge number.
In the active state, it is app's responsibility to handle the remote notification and change the badge icon.
Note : 'badge' property is enclosed in the 'aps' dictionary received within the payload. My payload looks like :
aps = {
alert = "Push notification!";
badge = 9;
sound = default;
}
You should define better what you want to do because it is not clear to me if wether you are talking about remote or local notifications, or what exactly you want to get and what are you getting.
Anyway, regarding remote notifications (APNS), take into account that you should set the badge as part of the payload if you want it to be set with the app closed. Your app does not get invoked unless the user manually opens the app (from the NotificationCenter or the dock).
On the other hand, when you are running the app, you will receive the call to
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
in your AppDelegate, and then you can set the badge by using
application.applicationIconBadgeNumber = badge;
Look at this, maybe it is a settings problem!:
application Icon Badge Number in ios5
Try also to give applicationIconBadgeNumber an int value, not NSInteger.
OK. Now my report about strange things.
I got it! It works good for now.
Problem was:
i created my App with push notification everything was ok with simple integer (1,8,99) but then i noticed that my integer 35.06 in the PHP variable $price did not recognize it at all even (int)$price unsuccessful . I thought the number is too long (but actually 4 digits is very popular). I decided to use
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
but as it was mentioned it does not work at all when the App is not started. No chance. It will never update the badge. I spent a lot of time thinking about what could be wrong and suddenly something came on my mind.
in the payload i just did this
'badge' => $price*100
an of course it worked good for me =)
I dont know what is the problem , why didn't work (int)$price (it is actually integer) but it is ok now.
My badge shows now something like this 3.506 and of course you can move the dot on step and you have your 35.06
Hope it could help you.

iphone Launch Options

im not getting any launch options after a push notification; heres the code i have but non of the NSLogs seem to print in the debug area.
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:#"aps"];
NSLog(#"Custom: %#", itemName);
} else {
NSLog(#"//////////////////////////");
}
when i open the app (via pressing view on the push notification) it goes to the didReceiveRemoteNotification script, im not sure if thats meant to happen..
thanks for reading.
Your application receives push notifications through multiple paths, depending on the state of your app when it is received.
If your app is not launched (not even suspended in background), the launchOptions will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey).
If it is already running or suspended in background, the app will receive the notifications via application:didReceiveRemoteNotification: in your application delegate.
The process is the same for local notifications (UIApplicationLaunchOptionsLocalNotificationKey in application:didFinishLaunchingWithOptions: and application:didReceiveLocalNotification:)
an error spotted:
NSDictionary *remoteNotif = [launchOptions objectForKey:
UIApplicationLaunchOptionsRemoteNotificationKey];
If you want to receive the remote notification, NSDictionary should be used not UILocalNotification
The remote notification is a payload, containing arguments, not a local notification. You might want to look at this url question:
Crash when handling remote notification when app not running
If you want to do local notification, change it like Ludovic's suggestion
The answers given above are correct. I largely use the following snippet in my application:didFinishLaunchingWithOptions:
if let remoteNotifPayload = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
notificationsController.didReceiveRemoteNotification(with: remoteNotifPayload)
}
If you want a local notification (I assume with your var name) replace UIApplicationLaunchOptionsRemoteNotificationKey by UIApplicationLaunchOptionsLocalNotificationKey and this should work.
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Alert message: %#",[[userInfo valueForKey:#"aps"] valueForKey:#"alert"]);
}
You can use NSUserDefaults to do the trick. In your AppDeligate.m, set a bool to YES in the first time. So after that it never gets to NO.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//add this if loop
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}