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.
Related
I'm able to send push notifications to my IOS device. But when I click on that notification it just opens the app.when my app is in background or in foreground it works perfectly, I want the app to open and navigate to a specific view controller depending on the push notification received.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Print message ID.
NSLog(#"Message ID: %#", userInfo[#"gcm.message_id"]);
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
NSDictionary *apsDict = [userInfo objectForKey:#"aps"];
NSString *alertForegroundMessage = [NSString stringWithFormat:#"%#", [apsDict objectForKey:#"alert"]];
// Pring full message.
NSLog(#"%#", userInfo);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
// Check result of your operation and call completion block with the result
completionHandler(UIBackgroundFetchResultNewData);
NSMutableArray *arrnotification;
arrnotification = [userInfo valueForKey:#"aps"];
});
if (application.applicationState == UIApplicationStateActive)
{
NSDictionary *userInfo = [[NSDictionary alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:#"downloadDataFromServer" object:self userInfo:userInfo];
NSLog(#"User Info : %#",userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
else
{
NSLog(#"userInfo->%#", [userInfo objectForKey:#"aps"]);
NSDictionary *sentObject = [NSDictionary dictionaryWithObjectsAndKeys:noteDict,#"data", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"downloadDataFromServer" object:sentObject];
completionHandler(UIBackgroundFetchResultNewData);
}
completionHandler(UIBackgroundFetchResultNewData);
}
When the app gets launched after the user opens the app from the notification. The best way to handle navigation based on the notification is to check the launchOptions dictionary in the application(_:didFinishLaunchingWithOptions:) method. This should contain the remoteNotification- key, which holds the push notification data.
As mentioned in the discussion section of application(_:didReceiveRemoteNotification:fetchCompletionHandler:) -method, documentation, the system does not automatically launch your app.
application(_:didFinishLaunchingWithOptions:)
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];
...
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary
*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Push info= %#",userInfo);
if([userInfo[#"aps"][#"content-available"] intValue] == 1)
{
NSLog(#"silent push notification received");
NSString *strQuery = [NSString stringWithFormat:#"UPDATE staff_list SET is_update =
'YES'"];
[[DatabaseManager sharedInstance] updateDatabaseForQuery:strQuery];
completionHandler(UIBackgroundFetchResultNewData);
return;
}
else
{
completionHandler(UIBackgroundFetchResultNoData);
return;
}
}
My payload for silent push notification is {
aps = {
"content-available" = 1;
sound = "";
};
}
Also unabled background mode for remote notification.
This will work perfect when app is running in foreground but when I press home button then app is running in background and after that not executing any method when receiving a new silent push notification.
Please help me I have stuck with this since last 3 days.
Thanks in advance.
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
}
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!!