折腾:
[记录]给iOS的企业开发者帐号的项目中集成极光推送JPUSH的消息推送服务
之后,就是把,之前已经验证工作了的代码,拷贝,集成到现在的项目中了。
经过调试和集成:
[记录]调试企业开发者账号的Development环境的JPUSH极光推送
验证development环境可以正常推送消息后。
相关部分的代码如下:
Login.swift
func gotoMainView() { gLog.verbose(“”) dispatchMain_async({ showMainView() SingletonMainVC().jpushInit() }) } |
MainViewController.swift
func jpushInit() { gLog.info(“init jpush”) //set alias let userIdAlias = String(gCurUserItem.id) gLog.debug(“userIdAlias=\(userIdAlias)”) //userIdAlias=10000010 jpushSetAlias(userIdAlias) } func jpushSetAlias(curAlias:String) { gLog.debug(“curAlias=\(curAlias)”) //curAlias=10000010 JPUSHService.setAlias(curAlias, callbackSelector: #selector(self.jpushSetAliasCallBack(_:tags:alias:)), object: self) } func jpushDeinit() { gLog.info(“”) //clear alias jpushSetAlias(“”) } func jpushSetAliasCallBack(resCode:CInt, tags:NSSet, alias:NSString) { gLog.debug(“resCode=\(resCode), alias=\(alias)”) //resCode=0, alias=10000010 //resCode=6002, alias= if resCode == 0 { gLog.info(“jpush set alias \(alias) ok”) } else { gLog.warning(“jpush set alias \(alias) failed”) } } |
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //enable notification -> othewise later set badge number will not work let notifSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil) application.registerUserNotificationSettings(notifSettings) //fixbug to make iPhone local notification work application.beginBackgroundTaskWithName(“showNotification”, expirationHandler: nil) // UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(WebsocketKeepConnectIntervalInSec) UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum) //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() //handle user tap remote notification if let options = launchOptions { if let userInfoDict = options[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary { gLog.debug(“userInfoDict=\(userInfoDict)”) //here not process the message //later will auto fetch new messages } } … } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { //2.recevie device token for register gLog.debug(“deviceToken=\(deviceToken)”) //2016-07-18 11:49:19.493 [Debug] [main] [AppDelegate.swift:488] didRegisterRemoteNotification > deviceTokenStr=Optional(<27b3528a f7e64b06 056c1524 048f90e9 3dbf21f3 bbc48a16 ebf6c11d a3852400>) NSNotificationCenter.defaultCenter().postNotificationName(“DidRegisterRemoteNotification”, object: deviceToken) //3. send device token to push server JPUSHService.registerDeviceToken(deviceToken) } func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { gLog.debug(“error=\(error.localizedDescription)”) } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { gLog.debug(“userInfo=\(userInfo)”) JPUSHService.handleRemoteNotification(userInfo) // NSNotificationCenter.defaultCenter().postNotificationName(“AddNotificationCount”, object: nil) NSNotificationCenter.defaultCenter().postNotificationName(“handleRemoteNotification”, object: userInfo) completionHandler(UIBackgroundFetchResult.NewData) } func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { gLog.debug(“notification=\(notification)”) // 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) { gLog.debug(“notificationSettings=\(notificationSettings)”) } @available(iOS 7, *) func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) { gLog.debug(“handleActionWithIdentifier=\(identifier), forLocalNotification=\(notification)”) } @available(iOS 7, *) func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) { gLog.debug(“handleActionWithIdentifier=\(identifier), forRemoteNotification=\(userInfo), withResponseInfo=\(responseInfo)”) } /************************************************************************* * JPUSH Functions *************************************************************************/ func networkDidSetup(notification:NSNotification) { gLog.debug(“已连接 networkDidSetup notification=\(notification)”) } func networkDidClose(notification:NSNotification) { gLog.debug(“已关闭 networkDidClose notification=\(notification)”) } func networkDidRegister(notification:NSNotification) { gLog.debug(“已注册 networkDidRegister notification=\(notification)”) if let info = notification.userInfo as? Dictionary<String,String> { // Check if value present before using it if let RegistrationID = info[“RegistrationID”] { gLog.debug(“RegistrationID=\(RegistrationID)”) } else { gLog.debug(“no value for key\n”) } } else { gLog.debug(“wrong userInfo type”) } } func networkDidLogin(notification:NSNotification) { gLog.debug(“已登陆 networkDidLogin notification=\(notification)”) if let registrationID = JPUSHService.registrationID() { gLog.debug(“registrationID=\(registrationID)”) } } func networkDidReceiveMessage(notification:NSNotification) { gLog.debug(“收到消息 networkDidReceiveMessage notification=\(notification)”) if let userInfo = notification.userInfo { if let title = userInfo[“title”] { gLog.debug(“title=\(title)”) } if let content = userInfo[“content”] { gLog.debug(“content=\(content)”) } if let ID = userInfo[“ID”] { print(“ID=\(ID)”) let extra = ID as! NSDictionary gLog.debug(“extra=\(extra)”) } } } func serviceError(notification:NSNotification) { let userInfo = notification.userInfo as? Dictionary<String,String> let error = userInfo![“error”] gLog.debug(“serviceError error=\(error)”) } func didRegisterRemoteNotification(notification:NSNotification) { gLog.debug(“已注册远程通知 didRegisterRemoteNotification=\(didRegisterRemoteNotification)”) let deviceTokenStr = notification.object gLog.debug(“deviceTokenStr=\(deviceTokenStr)”) } func listenRemotePush(){ gLog.debug(“”) let defaultCenter:NSNotificationCenter = NSNotificationCenter.defaultCenter() defaultCenter.addObserver(self, selector: #selector(AppDelegate.networkDidSetup(_:)), name:kJPFNetworkDidSetupNotification, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.networkDidClose(_:)), name:kJPFNetworkDidCloseNotification, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.networkDidRegister(_:)), name:kJPFNetworkDidRegisterNotification, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.networkDidLogin(_:)), name:kJPFNetworkDidLoginNotification, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.networkDidReceiveMessage(_:)), name:kJPFNetworkDidReceiveMessageNotification, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.serviceError(_:)), name:kJPFServiceErrorNotification, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.didRegisterRemoteNotification(_:)), name:”DidRegisterRemoteNotification”, object: nil) defaultCenter.addObserver(self, selector: #selector(AppDelegate.handleRemoteNotification(_:)), name:”handleRemoteNotification”, object: nil) } func handleRemoteNotification(remoteNotification:NSNotification) { gLog.debug(“remoteNotification=\(remoteNotification)”) /* remoteNotification=NSConcreteNotification 0x10c4ef820 {name = handleRemoteNotification; object = { “_j_msgid” = 4044282063; aps = { alert = “test push to ios “; badge = 1; sound = default; }; }} */ if let userInfoDict = remoteNotification.object { gLog.debug(“userInfoDict=\(userInfoDict)”) /* userInfoDict={ “_j_msgid” = 4044282063; aps = { alert = “test push to ios “; badge = 1; sound = default; }; } */ if let aps = userInfoDict[“aps”] { if let badge = aps?[“badge”] as? Int { gLog.debug(“badge=\(badge)”) //badge=1 UIApplication.sharedApplication().applicationIconBadgeNumber = badge } } if UIApplication.sharedApplication().applicationState == UIApplicationState.Active { gLog.debug(“not handle remote notification for app is active running”) } else { gLog.debug(“applicationState=\(UIApplication.sharedApplication().applicationState)”) // SingletonConversationTVC().updateAllConversation() } } } |
头文件:
SalesApp-Bridging-Header.h
#import “JPUSHService.h” |
注:
项目为添加推送修改了配置后,对应文件变化:
info.plist
<key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array> |
转载请注明:在路上 » [记录]把JPUSH消息推送到代码整合到现有项目中