Currently my app receives notifications about updates to an RSS feed. If the user opens the app from the notification, it opens to the correct view controller. If the user does not acknowledge the notification and opens the app from the app icon, when the user opens the menu within the app, the table view cell for that rss feed has a badge icon with the applicationIconBadgeNumber. Once that row is selected, the badge on that cell goes away and the applicationIconBadgeNumber is reset. My question is about wanting to send notifications about other info within the app such as member benefits. How can I differentiate which row in the table view gets the badge? Say the user gets a notification about a member benefit. I would want the badge to appear in the member benefits row of the table view, but if there is a notification from the RSS feed, badge the appropriate row.
Here is how I'm currently adding the badge for the RSS feed row.
in cellForRowAtIndexPath:
if (!(indexPath.row == 0))
{
cell.accessoryView = nil;
}
badgeNumber = [NSString stringWithFormat:#"%ld", (long)[[UIApplication sharedApplication]applicationIconBadgeNumber]];
actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber withStringColor:[UIColor whiteColor] withInsetColor:[UIColor redColor] withBadgeFrame:NO withBadgeFrameColor:[UIColor redColor] withScale:1.0 withShining:NO withShadow:NO];
actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);
if ([badgeNumber isEqualToString:#"0"])
{
actionAlertBadge.hidden = YES;
}
if (actionAlertBadge.hidden == NO)
{
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
cell.accessoryView = actionAlertBadge;
}
}
}
in didSelectRowAtIndexPath:
if (indexPath.row == 0)
{
ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[actionAlerts setWebViewController:wvc];
[[UAPush shared] resetBadge];
actionAlertBadge.hidden = YES;
[tableView reloadData];
navController = [[KFBNavControllerViewController alloc]initWithRootViewController:actionAlerts];
[UIView transitionWithView:appDelegate.window
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
appDelegate.window.rootViewController = navController;
}
completion:nil];
}
EDIT: Here is how I'm trying to accomplish this but it is not working because my notificationType string in my table view is NULL.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UA_LINFO(#"Received remote notification: %#", userInfo);
// Send the alert to UA so that it can be handled and tracked as a direct response. This call
// is required.
[[UAPush shared]appReceivedRemoteNotification:userInfo applicationState:application.applicationState];
// Optionally provide a delegate that will be used to handle notifications received while the app is running
// [UAPush shared].delegate = your custom push delegate class conforming to the UAPushNotificationDelegate protocol
// Reset the badge after a push received (optional)
[[UAPush shared] resetBadge];
NSDictionary *apsInfo = [userInfo valueForKey:#"aps"];
NSString *alertMsg = #"";
if ([apsInfo valueForKey:#"alert"] != NULL) {
alertMsg = [apsInfo valueForKey:#"alert"];
if ([alertMsg containsString:#"ACTION ALERT"]) {
notificationType = #"action alert";
}
else if ([alertMsg containsString:#"MEMBER BENEFIT"]) {
notificationType = #"member benefit";
}
}
}
cellForRowAtIndexPath:
KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate];
NSString *notificationType = appDelegate.notificationType;
// NSLog(#"notificationType menu table: %#", notificationType);
badgeNumber = [NSString stringWithFormat:#"%ld", (long)[[UIApplication sharedApplication]applicationIconBadgeNumber]];
actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber withStringColor:[UIColor whiteColor] withInsetColor:[UIColor redColor] withBadgeFrame:NO withBadgeFrameColor:[UIColor redColor] withScale:1.0 withShining:NO withShadow:NO];
actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);
if ([badgeNumber isEqualToString:#"0"]) {
actionAlertBadge.hidden = YES;
}
if (actionAlertBadge.hidden == NO) {
if ([notificationType isEqualToString:#"action alert"]) {
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.accessoryView = actionAlertBadge;
}
}
}
else if ([notificationType isEqualToString:#"member benefit"]) {
if (indexPath.section == 0) {
if (indexPath.row == 5) {
cell.accessoryView = actionAlertBadge;
}
}
}
}
In your AppDelegate, first make a property:
#property (nonatomic, copy) NSString *notificationType;
Then, in your
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
use:
self.notificationType = #"action alert";
self.notificationType = #"member benefit";
and in your CellForRowAtIndexPath:
if (actionAlertBadge.hidden == NO) {
if ([appDelegate.notificationType isEqualToString:#"action alert"]) {
....
}
else if ([appDelegate.notificationType isEqualToString:#"member benefit"]) {
....
}
On a side note, I would suggest trying to refrain from string comparisions and use something like enums.
First, while sending notification, you should add a field of type in the notification. You will get json dictionary like this
{"aps":{"alert":{"type":"rss","text":"Hello, world!"},"sound":"default","badge":3}}
Get the type value in function (if the app is running)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)dict {
// get the type information out of the dictionary
// now you can perform depending on this type.
}
If the app is not running,then according to apple's documentation
"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."
Get your dictionary in application:didFinishLaunchingWithOptions: method.
Related
I am developing a project in watch 2.0 using wcsession.
My actionable notification has two type of button that is accept and reject.When i click on accept or reject ActionWithIdentifier then my delegate method is not call.
I am using this delegate in extensiondeleget.m
1.- (void)handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification
{
dicPlayLoad=remoteNotification;
self.strActionNotificationType = identifier;
[self notification:identifier withDic:remoteNotification];
}
2.- (void)handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification withResponseInfo:(NSDictionary *)responseInfo
{
dicPlayLoad=remoteNotification;
self.strActionNotificationType = identifier;
[self notification:identifier withDic:responseInfo];
}
-(void)notification:(NSString *)strIdentifier withDic:(NSDictionary *)dic
{
self.strActionNotificationType = #"";
if ([strIdentifier isEqualToString:ACCEPT_IDENTIFIER])
{
NSDictionary *apsInfo = [dic objectForKey:#"aps"];
self.strStrRequestId = [apsInfo objectForKey:#"REQUEST_ID"];
self.strActionNotificationType = ACCEPT_PAYMENT_IDENTIFIER;
[self showAlertWithStyle:WKAlertControllerStyleAlert message:EXTENSION_DELGATE.strStrRequestId title:#"Alert"];
}
else if ([strIdentifier isEqualToString:REJECT_IDENTIFIER])
{
// do action for reject payment
NSDictionary *apsInfo = [dic objectForKey:#"aps"];
self.strActionNotificationType =isEqualToString:REJECT_IDENTIFIER;
// do action for accept payment
[self showAlertWithStyle:WKAlertControllerStyleAlert message:EXTENSION_DELGATE.strStrRequestId title:#"Alert"];
}
}
but when i show my strIdentifier in alert view it show empty string.
Hi i am new for push notification and i am working on a project there push notification have been used ,
i understand all code but not [[userInfo objectForKey:#"payload"] objectForKey:#"userId"]] where it can store value in userinfo ??
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*) userInfo {
**if ([self.profileManager.profile.userId isEqualToString:[[userInfo objectForKey:#"payload"] objectForKey:#"userId"]]**) {
if (application.applicationState == UIApplicationStateActive) {
UIRemoteNotificationType apnsType = [application enabledRemoteNotificationTypes];
if(apnsType != UIRemoteNotificationTypeNone) {
if((apnsType & UIRemoteNotificationTypeAlert) != 0) {
[self showNotificationAlert:userInfo];
}
if((apnsType & UIRemoteNotificationTypeBadge) != 0) {
NSString* badge = [userInfo valueForKey:#"badge"];
application.applicationIconBadgeNumber = [badge intValue];
}
}
} else {
NSString* event = [[userInfo objectForKey:#"payload"] objectForKey:#"event"];
if ([event isEqualToString:#"QUESTION_ANSWERED"]) {
[self presentYouAsked];
} else if ([event isEqualToString:#"QUESTION_ASKED"]) {
[self presentFriendsAsked];
}
}
}
}
The userInfo dictionary is part of the push notification data that was received. The line you refer to extracts another dictionary, associated with the key "payload" from the push notification dictionary and then looks for the value associated with the key "userid" in that dictionary.
At a guess that "if" statement it determining whether the push notification that was received applies to the current user on this device.
I handle push notification when the screen is on by automatically push to a detailViewController. It works fine when there is one or two push notification as I can push each detailViewController on top of each other and pop it back. However, app crash when I have to pop back more than two detailViewControllers. It happens only in iOS 7+.
Set notification Id
- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if(app.applicationIconBadgeNumber!=0)
{
app.applicationIconBadgeNumber = app.applicationIconBadgeNumber - 1;
}
NSString *aps =[NSString stringWithFormat:#"%#",[userInfo objectForKey:#"id"]];
if (app.applicationState == UIApplicationStateActive)
{
NSDictionary *dict = [NSDictionary dictionaryWithObjects:#[[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]] forKeys:#[#"notifyText"]];
[arrNotificationData addObject:dict];
[self showNotificationInActiveMode:[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]];
}
if ([aps isEqualToString:#"1"])
{
[UserDefault setpushnotificationid:[aps intValue]];
}
if ([aps isEqualToString:#"2"])
{
[UserDefault setpushnotificationid:[aps intValue]];
}
if ([aps isEqualToString:#"3"])
{
[UserDefault setpushnotificationid:[aps intValue]];
}
if ([aps isEqualToString:#"4"])
{
[UserDefault setpushnotificationid:[aps intValue]];
}if ([aps isEqualToString:#"5"])
{
[UserDefault setpushnotificationid:[aps intValue]];
}
}
Use singleton class as base class and check for ID and the push and pop the view.
#interface CustomViewController ()
#end
#implementation CustomViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(appBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
-(void)appBecomeActive
{
int val=[UserDefault getpushnotificationid];
if(val!=0){
[self checkforpushnotification];
}
}
-(void)checkforpushnotification
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
int val=[UserDefault getpushnotificationid];
[UserDefault setpushnotificationid:0];
//.: if current view is profile or setting just dismiss it.because they are present
switch (val)
{
case 1:
{
if( [self checkIfControllerExist:[DealsViewController class]])
{
return;
}else{
detailViewController *VC = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:#"VCView"];
[self.navigationController pushViewController:VC animated:YES];
}
}
break;
}
}
Now use bace class in your class
#interface detailViewController : CustomViewController
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!
I am trying to copy the contents of one NSDictionary into a NSMutableDictionary.
This is what I have done so far.
#implementation RosterListController
- (void)newMessageReceived:(NSDictionary *)messageContent
{
self.messageDictionary = [[NSMutableDictionary alloc]initWithDictionary:messageContent]; // This is where I am copying
UIApplication *app = [UIApplication sharedApplication];
if (app.applicationState == UIApplicationStateActive)
{
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
if (localNotif)
{
localNotif.alertAction = #"OK";
localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[app presentLocalNotificationNow:localNotif];
}
}
}
Then I handle this notification in appDelegate by showing a AlertView.After that is done..coming back to RosterViewController.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"Cancel button presed");
}
else
{
if(chatView) // Chat ViewController
{
[chatView recvdMsg:self.messageDictionary];
}
else
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
chatView=(ChatViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"chatviewcontroller"];
NSMutableDictionary *buddy=[[NSMutableDictionary alloc]init];
// The break point stops the app here & shows buddy dictionary as nil.
==> [buddy setObject:[self.messageDictionary objectForKey:#"sender"] forKey:#"jid"];
// Originally it was messageContent dict from the parameter above that I used which worked fine..
//But since I need that dictionary in another method..I copied that dictionary into self.messageDictionary which also gets copied.
// However now the above line causes problems.
// [buddy setObject:[self.messageContent objectForKey:#"sender"] forKey:#"jid"];
chatView.buddyData=buddy;
[self.navigationController pushViewController:chatView animated:YES];
[chatView recvdMsg:self.messageDictionary];
}
}
}
To create NSMutableDictionary from NSDictionary you should use dictionaryWithDictionary which is declared in NSDictionary.h.
NSMutableDictionary *aMutDictFromAnotherDictionary = [NSMutableDictionary dictionaryWithDictionary:YOUR_NSDICTIONARY_OBJECT];