乐趣区

关于ios: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;
    }
}
退出移动版