友盟IOS版本的推送之前是基于OBJC写的,友盟官方提供的API也是基于OBJC。而swift2.0下集成友盟推送功能并没有相关API。
网上的资料零零散散有一些。
下面是我实现的swift2.0下集成友盟推送功能:
1:首先要在swift下实现。需要swift和OBJC混编。
如图所示一个干净的swift项目
右键->New File ->选择Object-C File->随便起个名字 然后弹出以下界面
选择Create Bridging Header,XCode会自动给你建立一个头文件。这个头文件中可以import用OBJc写的友盟推送方法。
至此第一步让项目可以swift和OBJC混编完成。
2:给项目导入友盟插件。这个友盟官方API有。就不多说了!
3:Swift2.0项目集成友盟
首先在刚才XCode自动给我们生产的头文件中加入以下代码:
#import "UMessage.h"
然后在AppDelegate中的
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UMessage.startWithAppkey("你的友盟KEY", launchOptions: launchOptions)
if (UIDevice.currentDevice().systemVersion.compare("8.0.0") == .OrderedSame || UIDevice.currentDevice().systemVersion.compare("8.0.0") == .OrderedDescending){
let action1 = UIMutableUserNotificationAction();
action1.identifier = "action1_identifier"
action1.title = "Accept"
action1.activationMode = UIUserNotificationActivationMode.Foreground//当点击的时候启动程序
let action2 = UIMutableUserNotificationAction();//第二按钮
action2.identifier = "action2_identifier"
action2.title = "Reject"
action2.activationMode = UIUserNotificationActivationMode.Background
action2.authenticationRequired = true
action2.destructive = true
let categorys = UIMutableUserNotificationCategory();
categorys.identifier = "category1";//这组动作的唯一标示
categorys.setActions([action1, action2], forContext: UIUserNotificationActionContext.Default)
let userSettings = UIUserNotificationSettings(forTypes:[
.Badge,
.Sound,
.Alert],
categories: Set(arrayLiteral: categorys))
UMessage.registerRemoteNotificationAndUserNotificationSettings(userSettings)
} else {
UMessage.registerForRemoteNotificationTypes([.Badge, .Sound, .Alert])
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
UMessage.registerDeviceToken(deviceToken)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
UMessage.didReceiveRemoteNotification(userInfo
网友评论