MobPush iOS推送功能最佳实现推荐

推送注册

最佳实现,建议在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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MobTech袤博科技

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

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

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

打赏作者

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

抵扣说明:

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

余额充值