iOS 消息推送

一、本地消息推送

1、在AppDelegate.m 中注册通知

 //注册通知
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    //请求获取通知权限(角标,声音,弹框)
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //获取用户是否同意开启通知
            NSLog(@"request authorization successed!");
        }
    }];

    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@",settings);
    }];

2、在需要添加本地通知的页面添加头文件和通知的协议:

#import <UserNotifications/UserNotifications.h>

@interface MessagePushVC ()<UNUserNotificationCenterDelegate>

@end

3、在viewDidLoad方法中添加:

[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

4、添加消息内容

//文本消息通知
- (void)textLocationNotification
{
    //第二步:新建通知内容对象
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"标题";
    content.subtitle = @"副标题";
    content.body = @"通知的内容";
    content.badge = @1;
    UNNotificationSound *sound = [UNNotificationSound defaultSound];    //系统默认的提示声音
    //UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"];   //自定义提示声音
    content.sound = sound;

    //通知触发机制。(重复提醒,时间间隔要大于60s)
    UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

    //创建UNNotificationRequest通知请求对象
    NSString *requertIdentifier = @"RequestIdentifier1";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];

    //将通知加到通知中心
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"Error:%@",error);

    }];
}

//文本+图片消息通知
- (void)textAndImageLocationNotification
{
    NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"avator_smaller" ofType:@"jpg"];
    UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];

    //第二步:新建通知内容对象
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"标题";
    content.subtitle = @"副标题";
    content.body = @"通知的内容";
    content.badge = @1;
    content.attachments = @[imageAttachment];//虽然是数组,但是添加多个只能显示第一个

    UNNotificationSound *sound = [UNNotificationSound defaultSound];    //系统默认的提示声音
    //UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"];   //自定义提示声音
    content.sound = sound;

    //通知触发机制。(重复提醒,时间间隔要大于60s)
    UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

    //创建UNNotificationRequest通知请求对象
    NSString *requertIdentifier = @"RequestIdentifier2";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];

    //将通知加到通知中心
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"Error:%@",error);

    }];

}

//只有当前处于前台才会走,加上返回方法,使在前台显示信息
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

    completionHandler(UNNotificationPresentationOptionBadge|
                      UNNotificationPresentationOptionSound|
                      UNNotificationPresentationOptionAlert);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值