iOS: 本地通知&远程推送

本地推送

AppDelegate.m
注册:

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

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:setting];
    }

    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

    }

        return YES;
}

发送通知:

- (IBAction)addLocalNotification:(id)sender {

    //判断是否已经授权用户发送通知
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
        // 创建本地通知对象
        UILocalNotification *localNotifi = [UILocalNotification new];

        // 设置属性
        // 时间
        localNotifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];


        // 内容
        localNotifi.alertBody = @"今天不适合敲代码";

        // 声音
        localNotifi.soundName = UILocalNotificationDefaultSoundName;

        // 标记
        localNotifi.applicationIconBadgeNumber = 5;


        // 调度本地通知
        [[UIApplication sharedApplication]scheduleLocalNotification:localNotifi];


    } else{
        //用户注册通知,注册后才能收到通知,这会给用户一个弹框,提示用户选择是否允许发送通知
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound   categories:nil]];
    }

}

远程推送

程序推倒后台或是退出,会和应用后台服务器断开链接,收不到信息,但只要设备联网就可以苹果的APNs(Apple Push Notification service)服务器建立链接,依然可以收到远程推送的消息
这里写图片描述

步骤1:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值