How to manage notification when users click on badge

被刻印的时光 ゝ 提交于 2019-11-29 04:38:48
Shane Powell

You want to read Handling Local and Remote Notifications

Basically in your application delegate, you want to implement:

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

and

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

And process the launchOptions / userInfo for the notification data.

How I normally process the data is:

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary* userInfo =
        [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [self processRemoteNotification:userInfo];
    }
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [self processRemoteNotification:userInfo];
}

The format for userInfo is documented the The Notification Payload section.

e.g. the "aps" key will give you another NSDictionary, then looking up the "alert" key will give you the alert message that was displayed. Also, any custom data you send in the JSON payload will be in there as well.

NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alertMsg = @"";
NSString *badge = @"";
NSString *sound = @"";
NSString *custom = @"";

if( [apsInfo objectForKey:@"alert"] != NULL)
{
    alertMsg = [apsInfo objectForKey:@"alert"]; 
}


if( [apsInfo objectForKey:@"badge"] != NULL)
{
    badge = [apsInfo objectForKey:@"badge"]; 
}


if( [apsInfo objectForKey:@"sound"] != NULL)
{
    sound = [apsInfo objectForKey:@"sound"]; 
}

if( [userInfo objectForKey:@"Custom"] != NULL)
{
    custom = [userInfo objectForKey:@"Custom"];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!