iOS 推送的 小问题

iOS推送通知实践
在didFinishLaunchingWithOptions中接收不到远程推送消息的问题,我看书上说这个方法不太靠谱,有可能不会正确执行,可以用在消息中心中添加监控的方法来代替:
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(launchNotification:) 
                                                 name:@"UIApplicationDidFinishLaunchingNotification" 
                                               object:nil];

刚又推送了一遍,发现在didFinishLaunchingWithOptions又可以收到消息了,而且也监控到了。

didFinishLaunchingWithOptions:的第二个参数可以判断是以什么类型启动应用的,用UIApplicationLaunchOptionsRemoteNotificationKey可以判断是不是通过远程推送启动的:
NSDictionary *apsDict = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"aps"];
判断是否开启了 push 功能!


 UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone) // Disabled



2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]获取到允许得push推送类型。然后再调用registerForRemoteNotificationTypes进行修改。若要关闭程序得push服务,可调用unregisterForRemoteNotifications

、补充:以上想法以实现。补充部分代码。settingsData为tableview的数据源数组
a、获取系push设置,用于显示给用户

//push设置
NSMutableArray * pushOptions = [[NSMutableArray alloc] init];
UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"声音", @"name", @"0", @"status",nil];
if (notificationType & UIRemoteNotificationTypeSound) {
    [soundNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:soundNotice];
[soundNotice release];
NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:                                       @"提醒", @"name",                                       @"0",   @"status",                                       nil];
if (notificationType & UIRemoteNotificationTypeAlert) {
    [alertNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:alertNotice];
[alertNotice release];
NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:                                       @"标记", @"name",                                       @"0",   @"status",                                       nil];
if (notificationType & UIRemoteNotificationTypeBadge) {
    [badgeNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:badgeNotice];
[badgeNotice release];
NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:                                @"通知设置", @"groupName",                                pushOptions,    @"data",                                nil];
[self.settingsData addObject:pushConfig];
[pushOptions release];
[pushConfig release];
b、获取用户设置的数据放入pushdata,然后向系统提交设置

NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];
NSInteger length = [pushData count];
UIRemoteNotificationType myType = 0;
for (NSInteger i =0; i< length; i++) {
    if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1)
    {
        switch (i) {
            case 0:
                myTypemyType = myType|UIRemoteNotificationTypeSound;
                break;
            case 1:
                myTypemyType = myType|UIRemoteNotificationTypeAlert;
                break;
            case 2:
                myTypemyType = myType|UIRemoteNotificationTypeBadge;
                break;
            default:
                break;
        }
    }
}
if (myType != 0) {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];
}
else {
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
} 
以上方案暂未用于代码实现。。。。。。。。。。。。。。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值