0
votes


I got my push notification successfully in both background and foreground but my problem is when my application is in active state/foreground and push notification is arrived then i show this pushnotification message on alertview using didReceiveRemoteNotification method,

My alert view have two buttons
1) later
2) Ok.

If I press 1) "later" button then I want add this pushnotification message in notification area so after some time user can see and tap on that particular push notification and go with that and that record of push notification will remove from notification area.

2

2 Answers

1
votes

This is not possible. There is no API to access notification are of iOS.

Alternative What near alternative you can try is Local Notification. When user select later set Local Notification for that thing. You can add this Local Notification when user leave your app so that you don't get notification while user is continue with your application.

Better Approach The most general approach for this problem is Notification screen in app. Your application has one screen which has list of received notification so that user can check that in you app. I suggest you to go with this. Because this is most common and clear idea.

1
votes

You need to implement core data for Notifications but it can only happen when your Application is is Active state.

1-create a new identity everytime a new notification arrive. 2-save it.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *Notification = userInfo;
NSString *title = [(NSDictionary*)[(NSDictionary*)[Notification valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"title"];
    NSString *body = [(NSDictionary*)[(NSDictionary*)[Notification valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"body"];
XXNotification *objNotification = [XXNotification create];

    objNotification.title = title;
    objNotification.detail = body;
[XXNotification save:nil];
NSArray *arrNotification =[XXNotification allUnRead:nil];
    [UtilityFunctions setApplicationBadgeNumber:[arrNotification count]];//Utility functions is my class for common functions.

if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
    {
[UtilityFunctions showAlertView:title message:body delegate:self cancelButtonTitle:@"Ok" otherButtonTitle:@"Cancel" withTag:99 withAccessibilityHint:[NSString stringWithFormat:@"%@:|:%@", title,body]];
    }
    else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive || [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
    {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.userInfo = userInfo;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.alertBody = body;
        localNotification.fireDate = [NSDate date];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        if (![IsLocationSaved isEqualToString:@"NO"])
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationForShowingNotification object: nil userInfo:nil];
        }
    }

}

On showing the UIAlertView, on its click event either delete that notification in DB or make a bool in it as isRead and make it YES. then save it,

On Notifications list Query the notification from DB or only those whose isRead = NO.

That is the way I did it in My Application.