扬庆の博客

消息推送处理

字数统计: 517阅读时长: 2 min
2021/04/07 Share

iOS 推送消息处理

项目里经常会用接第三方消息推送, 极光, 个推 .. 等 .

消息弹框, 点击处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import UIKit

extension NSNotification.Name {
/// 推送消息类型NotificationName汇总
public static let jpush_notificationTypeOne: NSNotification.Name
= NSNotification.Name(rawValue: "jpush_notificationTypeOne")
}

class JPushNotificationManage: NSObject {

static let share: JPushNotificationManage = JPushNotificationManage()
private override init() {}

enum JPushNotificationType: Int {
case none = 0
}

func register(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
let entity = JPUSHRegisterEntity()

entity.types = Int(JPAuthorizationOptions.alert.rawValue) | Int(JPAuthorizationOptions.badge.rawValue) | Int(JPAuthorizationOptions.sound.rawValue)

JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)

// 注 JPushIsProduction =1 生产环境 =0 测试环境
JPUSHService.setup(withOption: launchOptions,
appKey: kGData.jPushAppKey,
channel: "App Store",
apsForProduction:kGData.jPushIsProduction)

JPUSHService.registrationIDCompletionHandler { code, registrationID in
if code == 0 {
dPrint("========= registrationID获取成功:\(registrationID ?? "")")
} else {
dPrint("========= registrationID获取失败")
}
}

// 处理通知
guard let options = launchOptions,
let userInfo = options[.remoteNotification] as? [AnyHashable: Any] else
{
UIApplication.shared.applicationIconBadgeNumber = 0
return
}

JPUSHService.setBadge(0)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) { [weak self] in
self?.handleRemoteNotification(userInfo: userInfo, click: true)
}

}

func registerDeviceToken(deviceToken: Data) {
JPUSHService.registerDeviceToken(deviceToken)
}

// 这里处理页面逻辑, 跳转
func handleRemoteNotification(userInfo: [AnyHashable: Any], click: Bool) {

guard let userInfo = userInfo as? [String: Any] else { return }
print(userInfo)

// 无效
guard userInfo.count > 0 else { return }
// let _ = userInfo["extraCustomerStockId"]
guard let typeStr = userInfo["extraMsgType"] as? String,
let typeInt = Int(typeStr),
let jtype = JPushNotificationType(rawValue: typeInt) else { return }

// 这里根据jtype类型, 点击跳转
guard click else {return} //点击才跳转
//跳转
jumpToAnyPage(pageId: Int)
}


}

private extension JPushNotificationManage {

}

extension JPushNotificationManage: JPUSHRegisterDelegate {
func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]!) {

}

@available(iOS 10.0, *)
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification?) {

}

//极光
@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)
handleRemoteNotification(userInfo: userInfo, click: false)
} else {
// 判断为本地通知
}

completionHandler(Int(JPAuthorizationOptions.alert.rawValue) | Int(JPAuthorizationOptions.badge.rawValue) | Int(JPAuthorizationOptions.sound.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 {

haveReceivedRemoteNotification(userInfo: userInfo)
JPUSHService.setBadge(0)
} else {
// 判断为本地通知
}
completionHandler() // 系统要求执行这个方法
}

func haveReceivedRemoteNotification(userInfo: Dictionary<AnyHashable, Any>) {
if userInfo.count == 0 { return }
JPUSHService.handleRemoteNotification(userInfo)
JPushNotificationManage.share.handleRemoteNotification(userInfo: userInfo, click: true)
}
}


CATALOG
  1. 1. iOS 推送消息处理