ionic集成极光推送

最近公司做APP重构的项目要用到推送,技术栈基于ionic,做的Hybrid开发(公司没有android以及ios工程师)。网上找了一些资料,也就只有极光的推送有Cordova插件,所以就选用了极光。开发的过程中android端还好,IOS端的着实的蛋疼。官方也没有详细的ionic集成极光推送的文档,网上其他的教程也大多不全或者不完整,尝试了好久,看了几篇教程,最后根据自己的项目,整理了一下。(本文只要讲解如何配置)

github官方文档:https://github.com/jpush/jpush-phonegap-plugin

AppKey:****************(这个不做介绍,网上教程一大堆)

covdova安装插件方法:
cordova plugin add jpush-phonegap-plugin --variable APP_KEY=你的appkey
安装完成后即可在app中直接调用

启动极光推送:

if(window.plugins && window.plugins.jPushPlugin){
        window.plugins.jPushPlugin.init();//启动极光推送
        window.plugins.jPushPlugin.setDebugMode(true); //调试模式
}

【重点来了】
坑爹的是ios平台,ios平台安装完插件之后,要打开Xcode配置SDK,
步骤:项目-Classes文件夹-AppDelegate.m,
在头部添加引入文件代码:

#import "JPUSHService.h"
#import "JPushPlugin.h"

然后修改函数:didFinishLaunchingWithOptions如下:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        #ifdef NSFoundationVersionNumber_iOS_9_x_Max
        NSSet *categories;
        //      entity.categories = categories;
            JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
            [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        #endif
          } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
              //可以添加自定义categories
              [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                                UIUserNotificationTypeSound |
                                                                UIUserNotificationTypeAlert)
                                                    categories:nil];
          } else {
              //categories 必须为nil
              [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                                UIRemoteNotificationTypeSound |
                                                                UIRemoteNotificationTypeAlert)
                                                    categories:nil];
          }
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

并添加以下函数:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

//处理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

//处理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
    
}

如果你觉得麻烦,或者是不理解上面的代码结构,你可以把AppDelegate.m中的代码全部删掉,复制以下代码并粘贴进去

/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

//
//  AppDelegate.m
//  your app_name
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "JPUSHService.h"
#import "JPushPlugin.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        #ifdef NSFoundationVersionNumber_iOS_9_x_Max
        NSSet *categories;
        //      entity.categories = categories;
            JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
            [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        #endif
          } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
              //可以添加自定义categories
              [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                                UIUserNotificationTypeSound |
                                                                UIUserNotificationTypeAlert)
                                                    categories:nil];
          } else {
              //categories 必须为nil
              [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                                UIRemoteNotificationTypeSound |
                                                                UIRemoteNotificationTypeAlert)
                                                    categories:nil];
          }
    

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

//处理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

//处理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
    
}

@end

然后再设置项目Capabilities中的Push Notifications为打开;Background Modes为打开,并勾选最后一项。(Remote notifications),如图:

ionic集成极光推送_第1张图片
image.png

然后再根据官方的介绍,引入依赖的Framework就行了。依赖如下图:

ionic集成极光推送_第2张图片
image.png

好了,到这里基本就完成了,根据JPush的官方API文档来调用吧~!
文档地址:https://github.com/jpush/jpush-phonegap-plugin

你可能感兴趣的:(ionic集成极光推送)