iOS基础2-通知

版权声明:Coder Bruce https://blog.csdn.net/bruceyou1990/article/details/80008135

本地通知

第一步 注册通知

在
didFinishLaunchingWithOptions
注册
 if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8

        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];

        [application registerUserNotificationSettings:setting];

    }

第二步 通知代理

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{          NSLog(@"收到本地通知 %@",notification);   

      };

在前台的时候回调 在后台显示通知不走这个

第三步 发送通知


    // 1.1.设置通知发出的时间
     UILocalNotification *localNote = [[UILocalNotification alloc] init];

    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];


    // 1.2.设置通知内容

    localNote.alertBody = @"您的手表已经失去连接";
    localNote.alertTitle = @"小位提醒";

    // 1.3.设置锁屏时,字体下方显示的一个文字

    localNote.alertAction = @"赶紧!!!!!";

    localNote.hasAction = YES;

    // 1.4.设置启动图片(通过通知打开的)

    //    localNote.alertLaunchImage = @"../Documents/IMG_0024.jpg";

    // 1.5.设置通过到来的声音
    localNote.soundName = UILocalNotificationDefaultSoundName;
    // 1.6.设置应用图标左上角显示的数字
    localNote.applicationIconBadgeNumber  = + localNote.applicationIconBadgeNumber;
    // 1.7.设置一些额外的信息

    localNote.userInfo = @{@"qq" : @"704711253", @"msg" : @"success"};
    // 2.执行通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

iOS10新增:处理前台收到通知的代理方法

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {//应用处于前台时的远程推送接受

    } else {//应用处于前台时的本地推送接受
        completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);//
    }
}

猜你喜欢

转载自blog.csdn.net/bruceyou1990/article/details/80008135