-》
-》
JPush极光推送-提供APNS云推送和Android Push服务
极光IM-移动IM云服务|即时聊天SDK|即时通讯云|APP应用内聊天解决方案
iOS SDK 集成指南 – 极光推送开发 – JPush 开发文档
后来看到了:
极光推送/IM JPush/JMesage · GitHub
里面有各种参考代码,包括Swift的:
GitHub – jpush/jpush-swift-demo: Offically supported Swift Demo for JPush iOS SDK.
以及参考:
去给App添加推送的设置:
Push Notifications
然后参考了代码后,自己加上代码:
/*************************************************************************** * JPUSH ***************************************************************************/ let JpushAppKey:String = “3a1a8xxxxxxa3” let JpushChannel:String = “iOS-AppStore” let JpushIsProduction:Bool = false func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { print(“didFinishLaunchingWithOptions”) // Override point for customization after application launch. 。。。 //1.reggister device JPUSHService.registerForRemoteNotificationTypes(UIUserNotificationType.Badge.rawValue | UIUserNotificationType.Sound.rawValue | UIUserNotificationType.Alert.rawValue , categories: nil) JPUSHService.setupWithOption(launchOptions, appKey: JpushAppKey, channel: JpushChannel, apsForProduction: JpushIsProduction) listenRemotePush() return true } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { //2.recevie device token for register print(“didRegisterForRemoteNotificationsWithDeviceToken”) print(“deviceToken=\(deviceToken)”) //deviceToken=<77366f0d c4a4f2f4 74fe24de 5db46132 c725ee1a 8e41b1fc a9a511bc 832c0113> NSNotificationCenter.defaultCenter().postNotificationName(“DidRegisterRemoteNotification”, object: deviceToken) //3. send device token to push server JPUSHService.registerDeviceToken(deviceToken) } func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { print(“didFailToRegisterForRemoteNotificationsWithError”) print(“error=\(error.localizedDescription)”) } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { //4. receive the remote push message from (APNS -> JPUSH) server print(“didReceiveRemoteNotification”) print(“userInfo=\(userInfo)”) JPUSHService.handleRemoteNotification(userInfo) NSNotificationCenter.defaultCenter().postNotificationName(“AddNotificationCount”, object: nil) } func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { print(“didReceiveLocalNotification”) print(“notification=\(notification)”) //notification=<UIConcreteLocalNotification: 0x15f158b60>{fire date = 2016年3月11日 星期五 中国标准时间 16:33:53, time zone = Asia/Shanghai (GMT+8) offset 28800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = (null)} JPUSHService.showLocalNotificationAtFront(notification, identifierKey: nil) // if let userInfo = notification.userInfo { // let newMessage = userInfo[“newMessage”] as! Message // debugPrintMsg(newMessage) // } } @available(iOS 7, *) func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { print(“didRegisterUserNotificationSettings”) print(“notificationSettings=\(notificationSettings)”) //notificationSettings=<UIUserNotificationSettings: 0x79070130; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> /* notificationSettings=<UIUserNotificationSettings: 0x12e6529a0; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> 2016-03-11 16:22:05.110 | JPUSH | W – [JPUSHClientController] Not get deviceToken yet. Maybe: your certificate not configured APNs? or current network is not so good so APNs registration failed? or there is no APNs register code? Please refer to JPush docs. 2016-03-11 16:22:05.113 | JPUSH | I – [JPUSHSessionController] sis is not on protect 2016-03-11 16:22:05.116 | JPUSH | I – [JPUSHAddressController] Action – sendSisRequest */ } @available(iOS 7, *) func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) { print(“handleActionWithIdentifier=\(identifier), forLocalNotification=\(notification)”) } @available(iOS 7, *) func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) { print(“handleActionWithIdentifier=\(identifier), forRemoteNotification=\(userInfo), withResponseInfo=\(responseInfo)”) } func networkDidSetup(notification:NSNotification) { print(“已连接 networkDidSetup notification=\(notification)”) } func networkDidClose(notification:NSNotification) { print(“已关闭 networkDidClose notification=\(notification)”) } func networkDidRegister(notification:NSNotification) { print(“已注册 networkDidRegister notification=\(notification)”) if let info = notification.userInfo as? Dictionary<String,String> { // Check if value present before using it if let RegistrationID = info[“RegistrationID”] { print(“RegistrationID=\(RegistrationID)”) } else { print(“no value for key\n”) } } else { print(“wrong userInfo type”) } } func networkDidLogin(notification:NSNotification) { print(“已登陆 networkDidLogin notification=\(notification)”) if let registrationID = JPUSHService.registrationID() { print(“registrationID=\(registrationID)”) } } func logDic(dic:NSDictionary)->String? { if dic.count == 0 { return nil } let tempStr1 = dic.description.stringByReplacingOccurrencesOfString(“\\u”, withString: “\\U”) let tempStr2 = dic.description.stringByReplacingOccurrencesOfString(“\””, withString: “\\\””) let tempStr3 = “\”” + tempStr2 + “\”” var tempData:NSData = (tempStr3 as NSString).dataUsingEncoding(NSUTF8StringEncoding)! let str = (String)(NSPropertyListSerialization.propertyListFromData(tempData, mutabilityOption:NSPropertyListMutabilityOptions.Immutable, format:nil, errorDescription: nil)) return str } func networkDidReceiveMessage(notification:NSNotification) { print(“收到消息 networkDidReceiveMessage notification=\(notification)”) if let userInfo = notification.userInfo { if let title = userInfo[“title”] { print(“title=\(title)”) } if let content = userInfo[“content”] { print(“content=\(content)”) } if let ID = userInfo[“ID”] { print(“ID=\(ID)”) let extra = ID as! NSDictionary print(“extra=\(extra)”) let extarDesc = logDic(extra) print(“extarDesc=\(extarDesc)”) } } } func serviceError(notification:NSNotification) { let userInfo = notification.userInfo as? Dictionary<String,String> let error = userInfo![“error”] print(“serviceError error=\(error)”) } func didRegisterRemoteNotification(notification:NSNotification) { print(“已注册远程通知 didRegisterRemoteNotification=\(didRegisterRemoteNotification)”) let deviceTokenStr = notification.object print(“deviceTokenStr=\(deviceTokenStr)”) } func listenRemotePush(){ let defaultCenter:NSNotificationCenter = NSNotificationCenter.defaultCenter() defaultCenter.addObserver(self, selector: “networkDidSetup:”, name:kJPFNetworkDidSetupNotification, object: nil) defaultCenter.addObserver(self, selector: “networkDidClose:”, name:kJPFNetworkDidCloseNotification, object: nil) defaultCenter.addObserver(self, selector: “networkDidRegister:”, name:kJPFNetworkDidRegisterNotification, object: nil) defaultCenter.addObserver(self, selector: “networkDidLogin:”, name:kJPFNetworkDidLoginNotification, object: nil) defaultCenter.addObserver(self, selector: “networkDidReceiveMessage:”, name:kJPFNetworkDidReceiveMessageNotification, object: nil) defaultCenter.addObserver(self, selector: “serviceError:”, name:kJPFServiceErrorNotification, object: nil) defaultCenter.addObserver(self, selector: “didRegisterRemoteNotification:”, name:”DidRegisterRemoteNotification”, object: nil) } |
折腾过后,可以连接,注册,登陆了。
didFinishLaunchingWithOptions 2016-03-12 10:35:48.933 | JPUSH | I – [JPUSHService] <div–<————————- JPush Log —————————- <div–<——————JPush SDK Version:2.1.0–build:346———- <div–<—————AppKey:3a1axxxx8da3—————- <div–<————————————————————– 2016-03-12 10:35:48.942 | JPUSH | I – [JPUSHClientController] Action – setup prevStoredUser=Optional(“15xxxxx4”) prevStotedPassword=Optional(“111111”) didRegisterForRemoteNotificationsWithDeviceToken deviceToken=<89b266fc 31bb2513 20a35909 03a31e9f c8127fef 4946138c 66df7769 617b1c0c> 已注册远程通知 didRegisterRemoteNotification=(Function) deviceTokenStr=Optional(<89b266fc 31bb2513 20a35909 03a31e9f c8127fef 4946138c 66df7769 617b1c0c>) didRegisterUserNotificationSettings notificationSettings=<UIUserNotificationSettings: 0x14dd5cf60; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> 2016-03-12 10:35:49.107 | JPUSH | I – [JPUSHSessionController] sis is not on protect 2016-03-12 10:35:49.109 | JPUSH | I – [JPUSHAddressController] Action – sendSisRequest 已连接 networkDidSetup notification=NSConcreteNotification 0x14dda9c20 {name = kJPUSHNetworkDidSetupNotification} 已登陆 networkDidLogin notification=NSConcreteNotification 0x14de7f470 {name = kJPUSHNetworkDidLoginNotification} registrationID=13165ffa4e0b5fb6ba3 2016-03-12 10:35:50.104 | JPUSH | I – [JPUSHDeviceTokenReport] upload device token success |
然后再去参考:
去给App添加远程通知的设置:
Remote Notifications
但是结果:
app处于active时,没有调用到对应的networkDidReceiveMessage
app处于background时,也没有调用networkDidReceiveMessage,
另外,也没有执行到:didReceiveRemoteNotification
不过无意间发现,之前设置的后台的函数,
performFetchWithCompletionHandler
倒是执行了
估计是服务器没有推送消息?
转载请注明:在路上 » 【整理】利用极光推送JPush实现消息推送