`

iOS 通知

    博客分类:
  • ios
阅读更多


#define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define _IPHONE80_ 80000

#define WSDeviceToken     @"WSDeviceToken"           // 设备token

 

// 注册远程通知

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        //register remoteNotification types
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"action1_identifier";
        action1.title=@"确定";
        action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
       
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2_identifier";
        action2.title=@"取消";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
        action2.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
        action2.destructive = YES;
       
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"category1";//这组动作的唯一标示
        [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
       
        UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
                                                                                     categories:[NSSet setWithObject:categorys]];
        [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];
       
    } else{
        //register remoteNotification types
        [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
         |UIRemoteNotificationTypeSound
         |UIRemoteNotificationTypeAlert];
    }
#else

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 // 点击推送进入app
    NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    // 点击通知启动时有远程通知,点击app图标启动时没有远程通知
    if (remoteNotification) {
       
        NSDictionary *aps = [remoteNotification objectForKey:@"aps"];
        NSString *alert = [aps objectForKey:@"alert"];
    }

}

 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
 
    NSString *nweToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                           stringByReplacingOccurrencesOfString: @">" withString: @""]
                          stringByReplacingOccurrencesOfString: @" " withString: @""];
    NSLog(@"%@", nweToken);
    NSString *token = [USER_DEFAULT objectForKey:WSDeviceToken];
    if (!token || ![token isEqualToString:nweToken]) {
        // 保存token至服务器
    }
}

 

// 收到远程通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSDictionary *aps = [userInfo objectForKey:@"aps"];
    NSString *alert = [aps objectForKey:@"alert"];
    DLog(@"通知内容:%@", alert);
    DLog(@"收到推送消息:%@", userInfo);
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    switch (state) {

// app正在前端运行
        case UIApplicationStateActive:
        {
            DLog(@"app active时可以回调此方法,但是手机通知栏没有通知跟声音");
        }
            break;
        case UIApplicationStateBackground:
        {
            DLog(@"UIApplicationStateBackground");
        }
            break;

// app 在后台运行时点击远程通知
        case UIApplicationStateInactive:
        {
            DLog(@"点击通知图标 时点通知启动");
        }
            break;
        default:
            break;
    }

}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics