MobPush iOS 推送功能最佳实现推荐
作者:MobTech袤博科技
- 2022 年 9 月 14 日 上海
本文字数:1157 字
阅读完需:约 4 分钟
推送注册
最佳实现,建议在 appdelegate 里对推送的环境和通知功能进行注册,内部方法为异步处理,不会阻塞主线程。参考如下代码:
#import <MobPush/MobPush.h>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// 设置推送环境 特别注意,setAPNsForProduction:YES 的时候,需要项目打成release包处理,不可以直接使用xcode的debug和release环境
#ifdef DEBUG [MobPush setAPNsForProduction:NO];#else [MobPush setAPNsForProduction:YES];#endif
//MobPush推送设置(获得角标、声音、弹框提醒权限) MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init]; configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert; [MobPush setupNotification:configuration];
return YES;
}复制代码
注册推送监听
注册监听,可以监听到关于推送消息的到达和点击,参考如下代码:
//此方法需要在AppDelegate的 didFinishLaunchingWithOptions 方法里面注册 可参考Demo[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];复制代码
处理回调,解析参数
在回调中处理 MobPush 关于通知的监听,注意如果应用处于后台或者被杀死状态,需要点击通知消息触发,参考如下代码:
// 收到通知回调- (void)didReceiveMessage:(NSNotification *)notification{ MPushMessage *message = notification.object;
// 推送相关参数获取示例请在各场景回调中对参数进行处理 // NSString *body = message.notification.body; // NSString *title = message.notification.title; // NSString *subtitle = message.notification.subTitle; // NSInteger badge = message.notification.badge; // NSString *sound = message.notification.sound; // NSLog(@"收到通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%ld,\nsound:%@,\n}",body, title, subtitle, (long)badge, sound); switch (message.messageType) { case MPushMessageTypeCustom: {// 自定义消息回调 } break; case MPushMessageTypeAPNs: {// APNs回调 } break; case MPushMessageTypeLocal: {// 本地通知回调
} break; case MPushMessageTypeClicked: {// 点击通知回调
} default: break; }}复制代码
划线
评论
复制
发布于: 刚刚阅读数: 6
MobTech袤博科技
关注
还未添加个人签名 2019.05.08 加入
还未添加个人简介










评论