jpush推送格式 swift_Swift3集成极光推送

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

1.准备工作

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

#import "JPUSHService.h"

// iOS10注册APNs所需头文件

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import

#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;

// 如果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) {

// 角标变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集成极光推送,自己集成的时候遇到了很多坑,所以分享出来,希望大家可以少浪费点大脑细胞☺。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值