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

本文详细介绍了iOS开发中的推送通知,包括本地通知和远程通知。本地通知无需网络连接,用户点击会打开应用,需获取用户权限。远程推送涉及原理、DeviceToken获取、用户点击监听及证书配置。文中通过实例解释了如何为已上线应用添加远程推送功能以及首次上架带有推送功能的appID配置流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

推送通知分为两种:本地推送通知(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. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值