美文网首页IOS网友们的篇章iOS
Swift3.0集成极光推送

Swift3.0集成极光推送

作者: 豆丶浆油条 | 来源:发表于2017-01-09 11:15 被阅读2524次

  现在很多程序都开始使用Swift开发了,但是第三方库大多数都是用OC写的,所以我们要使用Swift和OC混编。今天的内容主要讲Swift3.0集成极光推送。

1.准备工作

集成指南,极光上说的都很清楚,把创建应用和配置工程实现。SDK下载地址。在桥接头文件中添加

#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

2.Swift3.0集成

(1)AppDelegate.swift中添加代理

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,JPUSHRegisterDelegate {
}

(2)注册推送及处理应用未打开时收到推送消息

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        // 通知注册实体类
        let entity = JPUSHRegisterEntity();
        entity.types = Int(JPAuthorizationOptions.alert.rawValue) |  Int(JPAuthorizationOptions.sound.rawValue) |  Int(JPAuthorizationOptions.badge.rawValue);
        JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self);
        // 注册极光推送
        JPUSHService.setup(withOption: launchOptions, appKey: "845b93e08c7fa192df019c07", channel:"Publish channel" , apsForProduction: false);
        // 获取推送消息
        let remote = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Dictionary<String,Any>;
        // 如果remote不为空,就代表应用在未打开的时候收到了推送消息
        if remote != nil {
            // 收到推送消息实现的方法
            self.perform(#selector(receivePush), with: remote, afterDelay: 1.0);
        }
        
        return true;
    }

(3)实现代理方法

 // MARK: -JPUSHRegisterDelegate
    // iOS 10.x 需要
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
        
        let userInfo = notification.request.content.userInfo;
        if notification.request.trigger is UNPushNotificationTrigger {
            JPUSHService.handleRemoteNotification(userInfo);
        }
        completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
    }
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
       
        let userInfo = response.notification.request.content.userInfo;
        if response.notification.request.trigger is UNPushNotificationTrigger {
            JPUSHService.handleRemoteNotification(userInfo);
        }
        completionHandler();
        // 应用打开的时候收到推送消息
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo)
    }
   
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
        JPUSHService.handleRemoteNotification(userInfo);
        completionHandler(UIBackgroundFetchResult.newData);
    }
    // 接收到推送实现的方法
    func receivePush(_ userInfo : Dictionary<String,Any>) {
        // 角标变0
        UIApplication.shared.applicationIconBadgeNumber = 0;
        // 剩下的根据需要自定义
        self.tabBarVC?.selectedIndex = 0;
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName_ReceivePush), object: NotificationObject_Sueecess, userInfo: userInfo)
    }
}

我看网上没有人写用Swift3.0集成极光推送,自己集成的时候遇到了很多坑,所以分享出来,希望大家可以少浪费点大脑细胞☺。

相关文章

  • Swift3.0 集成极光推送

    1.前言 推送证书配置什么的都不多讲了,极光推送的开发文档里都有详细的介绍极光推送文档,因为官方的文档是OC版本的...

  • 实现iOS收到推送消息后跳到指定的页面

    ########这里离线推送用的极光推送,集成推送这里就不做说明了,根据极光官方文档集成基本没有什么问题。 ###...

  • 极光推送集成开发

    1.极光推送集成与设置 极光推送地址①注册极光推送账号。②在应用管理内按照步骤创建APP。③找到“文档——iOS—...

  • iOS-iOS10极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • iOS-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • Swift3.0集成极光推送

      现在很多程序都开始使用Swift开发了,但是第三方库大多数都是用OC写的,所以我们要使用Swift和OC混编。...

  • "_OBJC_CLASS_$_JPUSHService

    在集成极光推送的时候运行报错:

  • Android 推送跳转逻辑

    本文例子已极光推送为例,极光推送集成连接如下:https://docs.jiguang.cn/jpush/clie...

  • 极光后台推送响铃

    前言: 本教程不讨论极光推送的集成,请自行百度如何集成极光推送本教程适用于需要支持ios10以下的后台推送响铃对于...

  • 极光推送 集成 使用 Token Authentication

    iOS 设备集成推送,以前需要集成开发证书和生产证书,比较麻烦,现在极光推送集成了Token Authentica...

网友评论

    本文标题:Swift3.0集成极光推送

    本文链接:https://www.haomeiwen.com/subject/uzncbttx.html