ios 消息推送问题分析

1.介绍比较清晰全面的网址链接   点击打开链接

     楼主按照链接操作过程中,说明一下遇到的问题以提醒参考的小伙伴。

    问题1  为何创建 Provisioning Profile 文件的过程中选择的certificates这一步的证书中没有apns证书,只有 ios development类型的证书?

               答:创建 Provisioning Profile过程中选择App ID这一步选择了具有 apns服务的app id,这一步就足够了,已经说明这个app id支持ios 消息推送服务了,创建生成的Provisioning Profile如果选择了具有 apns服务的app id,最后生成的含有 Push Service服务,如下图示

                       


   问题2.  生成pem文件具体指令?

             答:原文图示如下

                    

                    指令说明 openssl x509 -in aps_development.cer -inform  der -out PushChatCert.pem    其中  aps_development.cer值得是证书文件名,因为作者证书名称为 aps_development .cer(注意空格)在linux指令下,自动在空格处加了转义斜杠。


扫描二维码关注公众号,回复: 10768043 查看本文章

     问题3. 按照文中步骤操作了,无法获取device Token?

               答:可能是未做ios 10最新推送的适配,包括代码和设置都需要适配

                       1)最新ios 10适配代码如下,AppDelegate.m文件中

                     

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //iOS10特有
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        // 必须写代理,不然无法监听通知的接收与点击
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                // 点击允许
                NSLog(@"注册成功");
                [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                    NSLog(@"%@", settings);
                }];
            } else {
                // 点击不允许
                NSLog(@"注册失败");
            }
        }];
    }else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
        //iOS8 - iOS10
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
        
    }else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
        //iOS8系统以下
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
    }
    // 注册获得device Token
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken
{
        NSLog(@"---Token--%@", pToken);
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"userInfo == %@",userInfo);
    NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil ,nil];
    
    [alert show];
}
// iOS 10收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    UNNotificationRequest *request = notification.request; // 收到推送的请求
    UNNotificationContent *content = request.content; // 收到推送的消息内容
    NSNumber *badge = content.badge;  // 推送消息的角标
    NSString *body = content.body;    // 推送消息体
    UNNotificationSound *sound = content.sound;  // 推送消息的声音
    NSString *subtitle = content.subtitle;  // 推送消息的副标题
    NSString *title = content.title;  // 推送消息的标题
    
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        NSLog(@"iOS10 前台收到远程通知:%@",userInfo);
        
    }
    else {
        // 判断为本地通知
        NSLog(@"iOS10 前台收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
    }
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
}
                    2)设置适配,如下图示

                    

          

                    注意Background Modes中勾选Remote Notification,如下图示

                   

                 

                     问题4. 可以获取Device Token,但是运行php脚本文件显示  Message successfully delivered,但是app无法收到推送消息?

               答:查看terminal中php脚本运行是否有警告报错,确认 $deviceToken 字段不要有空格,有空格会警告,删掉空格就可以成功推送了,ios app获取的deviceToken默认含有空格。

             

                  

                  

发布了18 篇原创文章 · 获赞 1 · 访问量 7678

猜你喜欢

转载自blog.csdn.net/leitingdulante/article/details/72819650