iOS 推送的实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

 

   

    // 处理远程推送

    if (launchOptions && [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {

        [self checkRemoteNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];

    }

    

    //后台(未激活状态)接到服务器消息时执行

    if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {

         添加接到推送消息时需求

    }

    //进图APP将对通知相应的提示去掉

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    

    // 开启远程推送

    [self openRemoteNotification];

    

    return YES;

}

 

// 开启推送

- (void)openRemoteNotification {

      // 开启推送

    if (!iOS7) {

        

        [[UIApplication sharedApplication] registerForRemoteNotifications];

        UIUserNotificationSettings* requestedSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge)  categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:requestedSettings];

        

    } else {

        

        // Enable APNS

        [[UIApplication sharedApplication]

         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

        

    }

    

}

 

#pragma mark - ANPS

注册成功 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    

    if (deviceToken) {

        NSString * deviceTokenString = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

    .............

        此处将deviceTokenString上传给服务器

        

    }

}

 

//接收到远程推送

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    

    // 远程推送处理

    [self checkRemoteNotification:userInfo];

    

}

注册设备失败

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error

{

    NSLog(@"Failed to get token, error: %@", error);

}

从后台点击APP图标进入     

- (void)applicationWillEnterForeground:(UIApplication *)application {

判断是否有接到消息通知    

    if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {

        添加接到推送消息时需求

    }

    

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

远程推送处理

- (void)checkRemoteNotification:(NSDictionary*)userInfo {

    

    if (userInfo == nil || [userInfo isKindOfClass:[NSNull class]]) {

        return;

    }

#if !TARGET_IPHONE_SIMULATOR

    

NSLog(@"remote notification: %@",[userInfo description]);

    

    NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

    NSString *alert = [apsInfo valueForKey:@"alert"];

    NSNumber *badge = [apsInfo valueForKey:@"badge"];此body中必须有 

    

    NSNumber *type = [userInfo valueForKey:@"type"];

    NSNumber *menu = [userInfo valueForKey:@"menu"];

    NSString *text = [userInfo valueForKey:@"text"];

    NSString *link = [userInfo valueForKey:@"link"];

    NSString *val  = [userInfo valueForKey:@"val"];

    

    if ((!menu || [menu isKindOfClass:[NSNull class]]) ||

        (!type || [type isKindOfClass:[NSNull class]]) ){

        return;

    }

    

    //APP icon消息提示的显示

    if ([apsInfo valueForKey:@"badge"] && ![[apsInfo valueForKey:@"badge"] isKindOfClass:[NSNull class]]) {

        if([self canSendNotifications]) {

            [UIApplication sharedApplication].applicationIconBadgeNumber = [[apsInfo valueForKey:@"badge"] integerValue];

        }

    }

    

    // 服务器喊我打开应用去干活啦

    if ([type integerValue] == 3) {

     

        NSMutableDictionary *apnsInfo = [NSMutableDictionary dictionaryWithDictionary:userInfo];

        [apnsInfo setObject:@([UIApplication sharedApplication].applicationState == UIApplicationStateActive) forKey:@"isActive"];

        

        // 推送通知

        [[NSNotificationCenter defaultCenter] postNotificationName:k_NOTI_APNS_PUSH object:apnsInfo];

 

    }

    

#endif

 

}

 

// 判断是否能接收推送

- (BOOL)canSendNotifications;

{

    if (iOS7) {

        return YES;

    }

    

    UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

    BOOL canSendNotifications = NO;

    if (notificationSettings.types == (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)) {

        canSendNotifications =YES;

    }

    return canSendNotifications;

}

 

转载于:https://www.cnblogs.com/leishuai/p/4571133.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现iOS推送,需要遵循以下步骤: 1. 创建APNS证书 首先,你需要在苹果开发者中心创建一个APNS证书。这个证书将用于安全地将消息发送到iOS设备。 2. 获取设备Token 每个iOS设备都有一个唯一的设备Token,用于标识该设备。你需要在你的应用中获取该设备Token,并将其发送到你的服务器。 3. 编写PHP代码 使用PHP编写代码,以便将消息发送到APNS服务器。你需要使用APNS证书和设备Token来建立连接,并将消息发送到APNS服务器。 以下是一个简单的PHP代码示例: ``` <?php // Put your device token here (without spaces): $deviceToken = 'YOUR_DEVICE_TOKEN_HERE'; // Put your private key's passphrase here: $passphrase = 'YOUR_PASSPHRASE_HERE'; // Put your alert message here: $message = 'Hello, world!'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'YOUR_APNS_CERTIFICATE.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array( 'alert' => $message, 'sound' => 'default' ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send the notification to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?> ``` 在上面的代码中,你需要将以下变量替换为你自己的值: - YOUR_DEVICE_TOKEN_HERE:你的设备Token。 - YOUR_PASSPHRASE_HERE:你的APNS证书密码。 - YOUR_APNS_CERTIFICATE.pem:你的APNS证书文件名和路径。 - $message:你要发送的消息。 4. 测试推送 运行PHP代码并测试推送。如果一切正常,你应该会收到一个推送通知。 注意:在生产环境中,你需要将APNS服务器地址更改为“gateway.push.apple.com”。在示例代码中,我们使用的是开发环境的APNS服务器地址“gateway.sandbox.push.apple.com”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值