-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate+EaseMob.swift
150 lines (114 loc) · 6.36 KB
/
AppDelegate+EaseMob.swift
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// AppDelegate+EaseMob.swift
// EaseMobDemo
//
// Created by wanglei on 15/6/17.
// Copyright (c) 2015年 Ray. All rights reserved.
//
import Foundation
extension AppDelegate {
func easemobApplication(application: UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject: AnyObject]?) {
EaseMob.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
#if DEBUG
EaseMob.sharedInstance().registerSDKWithAppKey(EMIMHelper.defaultHelper().appkey, apnsCertName: "ENJOY-Develop-APNS")
#else
EaseMob.sharedInstance().registerSDKWithAppKey(EMIMHelper.defaultHelper().appkey, apnsCertName: "ENJOY-Distribute-APNS")
#endif
EaseMob.sharedInstance().chatManager.removeDelegate(self)
EaseMob.sharedInstance().chatManager.addDelegate(self, delegateQueue: nil)
setUpNotifiers()
}
// MARK: - Private
private func setUpNotifiers() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackgroundNotif:", name: UIApplicationDidEnterBackgroundNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWillEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidFinishLaunching:", name: UIApplicationDidFinishLaunchingNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActiveNotif:", name: UIApplicationDidBecomeActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWillResignActiveNotif:", name: UIApplicationWillResignActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidReceiveMemoryWarning:", name: UIApplicationDidReceiveMemoryWarningNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWillTerminateNotif:", name: UIApplicationWillTerminateNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appProtectedDataWillBecomeUnavailableNotif:", name: UIApplicationProtectedDataWillBecomeUnavailable, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appProtectedDataDidBecomeAvailableNotif:", name: UIApplicationProtectedDataDidBecomeAvailable, object: nil)
}
func appDidEnterBackgroundNotif(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationDidEnterBackground(application)
}
}
func appWillEnterForeground(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationWillEnterForeground(application)
}
}
func appDidFinishLaunching(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationDidFinishLaunching(application)
}
}
func appDidBecomeActiveNotif(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationDidBecomeActive(application)
}
}
func appWillResignActiveNotif(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationWillResignActive(application)
}
}
func appDidReceiveMemoryWarning(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationDidReceiveMemoryWarning(application)
}
}
func appWillTerminateNotif(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationWillTerminate(application)
}
}
func appProtectedDataWillBecomeUnavailableNotif(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationProtectedDataWillBecomeUnavailable(application)
}
}
func appProtectedDataDidBecomeAvailableNotif(notification: NSNotification) {
if let application = notification.object as? UIApplication {
EaseMob.sharedInstance().applicationProtectedDataDidBecomeAvailable(application)
}
}
func didReceiveMessage(message: EMMessage!) {
if UIApplication.sharedApplication().applicationState == .Active {
// TODO: App在活动中(运行中)如何给提示? play sound? or other?
}else {
// Local Notification
self.p_showLocalNotificationWithMessage(message)
}
}
private func p_showLocalNotificationWithMessage(message: EMMessage) {
let options = EaseMob.sharedInstance().chatManager.pushNotificationOptions
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate()
var messageStr: String = ""
if options.displayStyle == .ePushNotificationDisplayStyle_messageSummary {
if let messageBody = message.messageBodies.first as? IEMMessageBody {
switch messageBody.messageBodyType {
case .eMessageBodyType_Text:
if let textMessageBody = messageBody as? EMTextMessageBody {
messageStr = textMessageBody.text
}
case .eMessageBodyType_Image:
messageStr = "图片"
default:
break
}
}
localNotification.alertBody = "ENJOY在线客服:\(messageStr)"
}else {
localNotification.alertBody = "ENJOY客服回复了您一条新消息"
}
localNotification.alertAction = "打开"
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName;
//发送通知
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}