Get JSON payload data into a string from push notifications - ios

Im trying to get my JSON payload data from a push notification into a string.
{
aps = {
alert = "BG push";
sound = ,
};
}
I researched on SO and on Parse and tried various ways including this Apple Push Notification with Sending Custom Data however my string reruns (null) or as in this example in the JSON format
I want the alert data "BG Push" in a string so I can put this in an alert view
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if ( application.applicationState == UIApplicationStateActive ){
// app was already in the foreground
[PFPush handlePush:userInfo]; //<-----userInfo returns payload data in JSON format
}
else {
// app was just brought from background to foreground
NSLog(#"App was in background and opened from Push message");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Megger"
message: [NSString stringWithFormat:#"%#", userInfo]
delegate:self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil];
[alert show];
}
}

NSString *alert = userInfo[#"aps"][#"alert"];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Megger"
message: [NSString stringWithFormat:#"%#", alert]
delegate:self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil];
[alertView show];

How about
NSDictionary *temp = userInfo[#"aps"];
NSString *message = temp[#"alert"];
If it doesn't work, add this line and let me know what you get
NSLog( #"%#\r%#\r%#", userInfo, temp, message );

Related

Extract "alert" text from push notification

I have the following code placed in my app delegate file. In theory, it should be extracting the message from the push notification and displaying it in a UIAlertview - yet it displays the title and the candle button, and nothing else. Can anybody see where I am going wrong?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
pushText = [userInfo objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"News", "")
message:pushText
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
The push payload has the following structure:
{
"aps" : {
"alert" : "Push notification text"
}
}
So you need to pull out the "aps" dictionary first and then you can retrieve the value for the "alert" key:
pushText = [[userInfo objectForKey:#"aps"] objectForKey:#"alert"];

UILocalNotification display alert on notification center when app is in foreground

I am working with UILocalNotification to notify users about their schedule. Everything is working fine but I'd like to know whether there is a way to display notification alert on notification center when the app is in foreground.
The alert will not be fire on running application main view.
Please help me.
Put this code in didReceiveRemoteNotification Method.
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSString *cancelTitle = #"OK";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Test"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:nil, nil];
[alertView show];
} else
{
//Do stuff that you would do if the application was not active
}

Handling Apple Push Notification Service

I'm using apple push notification service in my project.
Please follow the 2 ways of opening the app and handling this push notifications. In the second scenario I do not know how to handle it. Do you know how?
The push notification arrived to my device,
Scenario 1:
I clicked on the push notification.
The - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo function AppDelegate.m file catches this function.
Scenario 2:
I normally opened the device (by clicking on the app)
How can I handle the push notification?
The other answers show how to get the notification data when the user taps the notification.
The difference between the two nethods shown is that one is called when app is already running, either in foreground or background, while the other is called when app is not running at all.
On your second case, when the user doesn't tap the notification, the notification data isn't passed to the app when you open it with the launch Icon.
First scenario:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog (#"APNS: notification received: %#", userInfo);
NSString *message = nil;
id alert = [userInfo objectForKey:#"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:#"alert"];
}
if (message)
{
if (![message isEqualToString:#""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: #"notification"
message: message
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
}
Second scenario:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog (#"LAUNCH OPTIONS: %#",launchOptions);
id remoteNotificationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotificationValue)
{
NSString *message = nil;
id alert = [remoteNotificationValue objectForKey:#"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:#"alert"];
}
if (message)
{
if (![message isEqualToString:#""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: #"notification"
message: message
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
}
....
Of course you might want to make a special method that handles notifications and is called from both scenarios (with NSDictionary * parameter) so your code would be more readable. Sometimes APNS notifications are useful also when app is running - empty notification (with no payload) might be used to trigger the data synchronization with server to avoid polling for example.
You can get the arrived notifications when the app starts with the following code (e.g: in application:didFinishLaunchingWithOptions):
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
Here is a more thorough explanation: How to manage notification when users click on badge
You can handle that like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Checking if app was launched from the notification
if (launchOptions != nil) {
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil){
// Read dictionary and do something since the app
// was launched from the notification.
}
}
Here is an example of what the dictionary object contains
NSString *message = #"";
NSString *badge = #"";
NSString *sound = #"";
if([dictionary objectForKey:#"alert"]) {
message = [dictionary objectForKey:#"alert"];
}
if([dictionary objectForKey:#"badge"]) {
badge = [dictionary objectForKey:#"badge"];
}
if([dictionary objectForKey:#"sound"]) {
sound = [dictionary objectForKey:#"sound"];
}
Hope it helps!

What is incorrect about this Microsoft sample for iOS APNS notification in Azure Mobile Services?

I'm a new Objective C programmer and am following the directions here to set up push notification.
When I add the following "optional" code I get an error, and am unable to compile:
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
NSLog(#"%#", userInfo);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:
[[userInfo objectForKey:#"aps"] valueForKey:#”alert”] delegate:nil cancelButtonTitle:
#"OK" otherButtonTitles:nil, nil];
[alert show];
}
The error is "unexpected "#" in program", located here userInfo objectForKey:#"aps".
What is the correct way to write this code?
Checking the page, I could see that there's a minor mistake in the code:
In [[userInfo objectForKey:#"aps"] valueForKey:#”alert”] , where is #”alert” should be #"alert"
(” is a different character from ")
This should be enough to get rid of the error.
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
NSLog(#"%#", userInfo);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:
[[userInfo objectForKey:#"aps"] valueForKey:#"alert"] delegate:nil cancelButtonTitle:
#"OK" otherButtonTitles:nil, nil];
[alert show];
}

Having issues in calling didReceiveRemoteNotification

I am using following code,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//code here to handle call
//[[UIApplication sharedApplication] openURL:
// [NSURL URLWithString:#"tel:1-408-555-5555"]];
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"Teshjhjkhhjkhjkhjkhkjhkhkhkjhjkhjkkkjhjhhjhjkjt" message:#"Test" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert1 show];
[alert1 release];
}
but when application is open I can see the alert, but I want this alert when I press the view button in push message.
Try to implement in this format:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive)
{
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Did receive a Remote Notification"
message:[NSString stringWithFormat:#"The application received this remote notification while it was running:\n%#",
[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}

Resources