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"];
Related
I know how to show a push notifications UIAlertView if the app is in the background or foreground using the following code:
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = [ [userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [ [UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
However, I also want to show an alert view if the app was completely closed and the user wants to open the app via the push notifications.
How can I achieve this?
Thanks
When application launches as a result of a push notification, in other words, when the app was not running, you need to handle that case in [AppDelegate application:didFinishLaunchingWithOptions:] method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Push Notification" message:notification[#"aps"][#"alert"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
If the app is in background or foreground (active) state, you need to handle that case in [AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] method.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Push Notification" message:userInfo[#"aps"][#"alert"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else if (application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) {
// Do something else rather than showing an alert view, because it won't be displayed.
}
}
For more information, you can take a look at Local and Remote Notification Programming Guide from Apple.
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 );
Hi Im currently developing an app where i have push notifications activated. I use parse.com. I have got it working so far that i can send a notification and the device receives it and i also get a badge on the app. And i have set an AlertView when you enter the app from the notification. But i dont know how to display the text of the push notification in the UIAlertView. And i also want the badge to disappear when you've viewed the message. Here is the code Im using:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateInactive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Text" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
else if (application.applicationState == UIApplicationStateActive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Text" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
{
NSDictionary * pushDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushDictionary)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Text" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
To handle the Push Notification and show an Alert View with its info try in appDelegate:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
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];
}
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];
}
}