极光推送

2017-01-20  本文已影响16人  虫yu
AppDelegate中:
#import <JPUSHService.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //设置push
    [self setupPush:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
- (void)setupPush:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    // * @param isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.
    [JPUSHService setupWithOption:launchOptions appKey:JPushKey
                          channel:@""
                 apsForProduction:YES
            advertisingIdentifier:nil];
    [application setApplicationIconBadgeNumber:0];// 去掉提示红点
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    [JPUSHService handleRemoteNotification:userInfo];
    [self handlerPush:application addParam:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    //自定义处理push内容
    [self handlerPush:application addParam:userInfo];
}
-(void)handlerPush:(UIApplication *)application addParam:(NSDictionary*)userInfo{
    [application setApplicationIconBadgeNumber:0];// 去掉提示红点
    
    if(application.applicationState == UIApplicationStateActive){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您有新消息到达" message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] delegate:self cancelButtonTitle:@"忽略此消息" otherButtonTitles:@"现在查看", nil];
        alert.tag = 666;
        [alert show];
    }else if(application.applicationState == UIApplicationStateInactive){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotification" object:nil];
    }
}
#pragma mark - UIAlertView Delegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == 666 && buttonIndex == 1){
         [[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotification" object:nil];
    }
}
然后在需要的页面注册通知,写需要app执行的方法
上一篇下一篇

猜你喜欢

热点阅读