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

推送注册

最佳实现,建议在appdelegate里对推送的环境和通知功能进行注册,内部方法为异步处理,不会阻塞主线程。参考如下代码:

#import 
- (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;
    }
}

你可能感兴趣的:(ios消息推送)