ionic集成极光推送

作者: 2003bc1d3f96 | 来源:发表于2017-09-09 17:11 被阅读145次

最近公司做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<UNNotificationCategory *> *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<UNNotificationCategory *> *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),如图:

image.png

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

image.png

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

相关文章

  • ionic集成极光推送

    最近公司做APP重构的项目要用到推送,技术栈基于ionic,做的Hybrid开发(公司没有android以及ios...

  • Ionic 集成极光 推送

    https://github.com/jpush/jpush-phonegap-plugin Install通过 ...

  • ionic3 集成极光推送

    不知道为什么,最新版本的jpush,注册不上用户,用的是3.3.4的版本,首先注册极光。 第一步,添加androi...

  • 实现iOS收到推送消息后跳到指定的页面

    ########这里离线推送用的极光推送,集成推送这里就不做说明了,根据极光官方文档集成基本没有什么问题。 ###...

  • ionic极光推送

    首先,极光官网注册账号 需要去这里注册https://www.jiguang.cn 注册成功获取AppKey 备注...

  • ionic极光推送

    问题描述:通过后台发送通知给个人。 解决方法: 1 新建账号 https://www.jiguang.cn/ 2 ...

  • 极光推送集成开发

    1.极光推送集成与设置 极光推送地址①注册极光推送账号。②在应用管理内按照步骤创建APP。③找到“文档——iOS—...

  • iOS-iOS10极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • iOS-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • "_OBJC_CLASS_$_JPUSHService

    在集成极光推送的时候运行报错:

网友评论

    本文标题:ionic集成极光推送

    本文链接:https://www.haomeiwen.com/subject/qqcujxtx.html