Parse push notification: How to use custom payload - ios

I'm using the following json to send a push notification in Parse.
{
"alert": "Push title goes here",
"sound": "",
"url": "emap://video/4000"
}
The url emap://video/4000 points to a specific video inside the app if I type that in Safari and hit enter. I want the user to be sent to this video when he clicks on the notification. Why doesn't the above JSON achieve this?

So say we are sending this payload :
NSDictionary *data = #{
#"alert" : #"some generic message here",
#"badge" : #"Increment",
#"sound" : #"default",
#"url" : #"emap://video/4000"
};
And when the user interacts with it act accordingly :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateInactive) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
-(void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
if (payload) {
NSString *urlLink = [payload valueForKey:#"url"];
// perform segue or tab bar selectedIndex or open webView whatever you want after checking if user is launching from notification :
}
}
You should also call this in application didFinishLaunchingWithOptions: in case the users app has been terminated or released from memory:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
[self handleRemoteNotificationWithPayload:notificationPayload];
...
}

Related

Push notification - alert missing from payload

This is the userInfo I am getting in didReceiveRemoteNotification
aps = {
badge = 2;
sound = default;
};
u = "{\"custom\":{\"redirectlink\":\"groupdetail.html?groupid=314416&selectedtabid=5\"}}";
}
Clearly alert field is missing here. This is what I tried to make to handle missing alert :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
NSMutableDictionary *pushDict = [NSMutableDictionary dictionaryWithDictionary:[userInfo objectForKey:#"aps"] ];
BOOL isSilentPush = [[pushDict objectForKey:#"content-available"] boolValue];
[pushDict setObject:#"You have a notification" forKey:#"alert"];
NSMutableDictionary * mut = [NSMutableDictionary dictionaryWithDictionary:userInfo]; //[userInfo copy];
[mut setObject:pushDict forKey:#"aps"];
//[mut setObject:#"5#" forKey:#"p"];
if (isSilentPush) {
NSLog(#"Silent push notification:%#", userInfo);
//load content here
// must call completionHandler
completionHandler(UIBackgroundFetchResultNewData);
}
else {
[[PushNotificationManager pushManager] handlePushReceived:mut];
// must call completionHandler
completionHandler(UIBackgroundFetchResultNoData);
}
}
But this does not generate a banner notification. Only badge, sound and alert is generated.
I know this should be handled from server side and not from iOS application, but the server guy will be back in 2 days and I have a demo to show.

Open specified URL in the Webview (iOS push)

in my iOS App i have an UIWebView so how i can open a specified URL in the UIWebView, with a push notification?
If somebody open the App with the notification, i want to show a specified Website in
the UIWebView.
Can I bind the URL (in background) with the push notification?
Thank you.
According to Apple...
If the app is running and receives a remote notification, the app
calls this method to process the notification. Your implementation of
this method should use the notification to take an appropriate course
of action. ... If the app is not running when a push notification
arrives, the method launches the app and provides the appropriate
information in the launch options dictionary. The app does not call
this method to handle that push notification. Instead, your
implementation of the application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
So, there are three possible scenarios:
1) App is in foreground: you will have full control, just implement didReceiveNotification and do whatever you want.
2) App is running, but in background: the action won't be triggered until the user actually open your app using the received notification.
3) The app is not running: in this case you should implement didFinishLaunchingWithOptions in order to get the additional info and perform the task.
So the code should look like this for didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = [launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
if(apsInfo) {
// Get the URL or any other data
}
}
And this is an approximation for didReceiveNotification
/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSString *message = nil;
id aps = [userInfo objectForKey:#"aps"];
if ([aps isKindOfClass:[NSDictionary class]]) {
message = [aps objectForKey:#"alert"];
}
if (message) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Notificación"
message:message
delegate:self
cancelButtonTitle:#"Aceptar"
otherButtonTitles:nil, nil];
[alertView show];
}
}
// Aditional data
NSString *url = [userInfo objectForKey:#"url"];
NSLog(#"Received Push URL: %#", url);
if(url!=nil)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
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);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:#"badge"];
NSLog(#"Received Push Badge: %#", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:#"badge"] integerValue];
#endif
}

Add multiple Push notifications to Global Array ios

I have a problem handling more than one Push Notifications. When I unlock the iphone and there is (for example) 10 push notifications of my app, my app just register some:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(#"Launched from push notification: %#", dictionary);
[self addMessageFromRemoteNotification:dictionary updateUI:YES];
}
}
}
If my app is open there is no problem and all received notifications are added to array.
Thank you

Add alertview in application when you recive push notification

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. But when i open the nothing happens and the badge does not disappear. I've set it in my appdelegate.m so parse is handling the push notifications. Heres some code that im using:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
[currentInstallation saveInBackground];
}
and also:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
Thanks
Ok thaks but how to i show the content of the push notification in the alert view? Or is that not a problem do i just have to configure a alertview with no text?
There are two scenarios:
App closed
When the app is closed and the user taps on a notification it's like when taps on the app icon. The app starts from application:didFinishLaunchingWithOptions:. To know if the app is opened normal (tap icon) or with a notification, just check the dictionary launchOptions for the key UIApplicationLaunchOptionsRemoteNotificationKey.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary * pushDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushDictionary)
{
//AlertView
}
}
App opened
When the app is opened there are two possible case
App in background UIApplicationStateInactive
App in foreground UIApplicationStateActive
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateInactive)
{
//AlertView
}
else if (application.applicationState == UIApplicationStateActive)
{
// AlertView
}
}
pushDictionary and userInfo are exactly the same dictionary that represents the notification.
Edit 1
A push notification is a JSON file that iOS automatically convert into a NSDictionary. The standard configuration is:
{"aps":
{
"alert":"Text message",
"sound":"default",
"badge":"1" //the number shown on the app's icon
}
}
From this base you can extend the JSON and put inside your own content. For example
{"aps":
{
"alert":"Text message",
"sound":"default",
"badge":"1" //the number shown on the app's icon
},
"Name":"Fry"
}
Now you can retrive the name in this very simple way:
NSString * name = userInfo[#"Name"];
and show it in the alert.
Edit 2
To show the content of push in UIAlertView just do this:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:name
message:name
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[av show];
If you receive push notification while your app isn't active (killed)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
will not be triggered. Instead of that you will have to use object with key UIApplicationLaunchOptionsRemoteNotificationKey from launchOptions dictionary inside
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

cannot receive push notification when app is not running

I've created an iphone app with push notification feature. The server works well and I could receive the notification when app is running in foreground. The function
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
is called then notification arrives.
However, when I press the home key, brought the app into background or kill it in the task bar. I cannot receive any notification, neither in the notification area or any popup.
Anyone knows the reason? Thanks
You can use...
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
if (launchOptions != nil) {
NSMutableDictionary *dic = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSMutableDictionary *dicItem = [dic objectForKey:#"aps"];
NSString *itemNotification = [dicItem objectForKey:#"alert"];
}else if (launchOptions == nil){
NSLog(#"launch ,,, nil");
}
...//code something
}
itemNotification is an item in Notification barge. Have fun!!

Resources