iOS开发 ☞ 推送通知(Push Notification)

推送通知分为两种:本地推送通知(Local Notification) 和 远程推送通知(Remote Notification)
一、本地通知
无需网络连接
当应用处于非前台状态的时候(无论是锁屏、后台、还是未运行),如果用户点击了通知,便会打开应用程序。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//iOS8.0 以后,发送本地通知需要添加的代码,效果见下图
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.f) {

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

        [application registerUserNotificationSettings:settings];
    }
    return YES;
}

必须获取用户权限:
这里写图片描述


发送通知代码:
- (IBAction)pressSend:(id)sender {

    UILocalNotification *local = [[UILocalNotification alloc] init];
    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.f];
    local.alertBody = @"我是本地通知";
    local.applicationIconBadgeNumber = 999;
    [[UIApplication sharedApplication] scheduleLocalNotification:local];
}

其他属性:

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

// timer-based scheduling
@property(nullable, nonatomic,copy) NSDate *fireDate;
// the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值