Skip to content

Commit

Permalink
增加IOS版本插件实现
Browse files Browse the repository at this point in the history
  • Loading branch information
ahzmr committed Mar 28, 2017
1 parent bd713ac commit 1764749
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 26 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
[![release](https://img.shields.io/badge/release-0.1.0-blue.svg)](https://github.com/ParadiseHell/mipush-cordova-plugin)
[![platforms](https://img.shields.io/badge/platforms-Android-lightgrey.svg)](https://github.com/ParadiseHell/mipush-cordova-plugin)

支持Android的Cordova小米推送插件
支持Android、IOS的Cordova小米推送插件

## 集成步骤

- 通过 Cordova Plugins 安装,要求 Cordova CLI 5.0+:

cordova plugin add mipush-cordova-plugin --variable PACKAGE_NAME=your_android_project_package_name --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid
cordova plugin add mipush-cordova-plugin --variable PACKAGE_NAME=your_android_project_package_name --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey

- 通过Url安装:

cordova plugin add https://github.com/ParadiseHell/mipush-cordova-plugin.git --variable PACKAGE_NAME=your_android_project_package_name --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid
cordova plugin add https://github.com/ParadiseHell/mipush-cordova-plugin.git --variable PACKAGE_NAME=your_android_project_package_name --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey

- 或者下载本地安装

cordova plugin add Your_Plugin_Path --variable PACKAGE_NAME=your_android_project_package_name --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid
cordova plugin add Your_Plugin_Path --variable PACKAGE_NAME=your_android_project_package_name --variable MI_PUSH_APP_KEY=your_mipush_appkey --variable MI_PUSH_APP_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_ID=your_mipush_appid --variable MI_PUSH_APP_IOS_KEY=your_mipush_appkey

## Demo

Expand All @@ -40,7 +40,7 @@

### 具体的 API 请参考:

- [Android API](/doc/android_api.md)
- [API](/doc/api.md)

## 常见问题

Expand Down
2 changes: 1 addition & 1 deletion doc/android_api.md → doc/api.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Android API说明
# API说明

- [注册小米推送](#注册小米推送)
- [注册成功时获取的内容](#注册成功时获取的内容)
Expand Down
39 changes: 38 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<preference name="PACKAGE_NAME" />
<preference name="MI_PUSH_APP_KEY"/>
<preference name="MI_PUSH_APP_ID" />
<preference name="MI_PUSH_APP_IOS_KEY"/>
<preference name="MI_PUSH_APP_IOS_ID" />

<!-- dependencies -->
<dependency id="cordova-plugin-device" />
Expand All @@ -24,6 +26,41 @@
<clobbers target="MiPush" />
</js-module>

<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="MiPushPlugin">
<param name="ios-package" value="MiPushPlugin" />
<param name="onload" value="true" />
</feature>
</config-file>
<config-file target="*-Info.plist" parent="MiSDKAppID">
<string>$MI_PUSH_APP_IOS_ID</string>
</config-file>
<config-file target="*-Info.plist" parent="MiSDKAppKey">
<string>$MI_PUSH_APP_IOS_KEY</string>
</config-file>
<config-file target="*-Info.plist" parent="MiSDKRun">
<string>Online</string>
</config-file>

<header-file src="src/ios/Plugins/MiPushPlugin.h" />
<source-file src="src/ios/Plugins/MiPushPlugin.m" />
<header-file src="src/ios/Plugins/AppDelegate+MiPush.h" />
<source-file src="src/ios/Plugins/AppDelegate+MiPush.m" />

<header-file src="src/ios/lib/MiPushSDK.h" />
<source-file src="src/ios/lib/libMiPushSDK.a" framework="true" />

<framework src="UserNotifications.framework" weak="true" />
<framework src="libresolv.dylib" weak="true" />
<framework src="libxml2.dylib" weak="true" />
<framework src="libz.dylib" weak="true" />
<framework src="SystemConfiguration.framework" weak="true" />
<framework src="MobileCoreServices.framework" weak="true" />
<framework src="CFNetwork.framework" weak="true" />
<framework src="CoreTelephony.framework" weak="true" />
</platform>

<platform name="android">
<config-file target="config.xml" parent="/*">
<feature name="MiPushPlugin">
Expand Down Expand Up @@ -94,7 +131,7 @@
<meta-data android:name="MiPushAppId" android:value="$MI_PUSH_APP_IDmipush"></meta-data>
</config-file>

<source-file src="src/android/MiPush_SDK_Client_3_0_3.jar" target-dir="libs" />
<source-file src="src/android/MiPush_SDK_Client_3_2_2.jar" target-dir="libs" />
<source-file src="src/android/MiPushPlugin.java" target-dir="src/com/ct/cordova/mipush" />
<source-file src="src/android/MiPushReceiver.java" target-dir="src/com/ct/cordova/mipush" />

Expand Down
Binary file removed src/android/MiPush_SDK_Client_3_0_3.jar
Binary file not shown.
Binary file added src/android/MiPush_SDK_Client_3_2_2.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions src/ios/Plugins/AppDelegate+MiPush.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// AppDelegate+MiPush.h
// delegateExtention
//
// Created by wenin819 on 17/3/26.
//

#import "AppDelegate.h"
#import "MiPushSDK.h"

@interface AppDelegate (MiPush) <MiPushSDKDelegate,
UNUserNotificationCenterDelegate>
@end
151 changes: 151 additions & 0 deletions src/ios/Plugins/AppDelegate+MiPush.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//
// AppDelegate+MiPush.m
// delegateExtention
//
// Created by wenin819 on 17/3/26.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "AppDelegate+MiPush.h"
#import "MiPushPlugin.h"

@implementation AppDelegate (MiPush)

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.viewController = [[MainViewController alloc] init];
BOOL rs = [super application:application didFinishLaunchingWithOptions:launchOptions];

[MiPushSDK registerMiPush:self type:0 connect:YES];

// 点击通知打开app处理逻辑
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo){
NSLog(@"%@", userInfo);
[MiPushPlugin onNotificationMessageClickedCallBack:userInfo];
}

return rs;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

#pragma mark 注册push服务.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"APNS token: %@", [deviceToken description]);

// 注册APNS成功, 注册deviceToken
[MiPushSDK bindDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(@"APNS error: %@", err);

// 注册APNS失败.
// 自行处理.
}

#pragma mark Local And Push Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"APNS notify: %@", userInfo);

// 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回
[MiPushSDK handleReceiveRemoteNotification:userInfo];
}

// iOS10新加入的回调方法
// 应用在前台收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"APNS notify: %@", userInfo);
[MiPushSDK handleReceiveRemoteNotification:userInfo];
}
// completionHandler(UNNotificationPresentationOptionAlert);
}

// 点击通知进入应用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"APNS notify: %@", userInfo);
[MiPushSDK handleReceiveRemoteNotification:userInfo];
}
completionHandler();
}

#pragma mark MiPushSDKDelegate
- (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
{
NSLog(@"command succ(%@): %@", [self getOperateType:selector], data);

if ([selector isEqualToString:@"registerMiPush:"]) {
NSLog(@"registerMiPush");
}else if ([selector isEqualToString:@"registerApp"]) {
// 获取regId
[MiPushPlugin onReceiveRegisterResultCallBack:data[@"regid"]];
}else if ([selector isEqualToString:@"bindDeviceToken:"]) {
// 获取regId
[MiPushPlugin onReceiveRegisterResultCallBack:data[@"regid"]];
}else if ([selector isEqualToString:@"unregisterMiPush"]) {
NSLog(@"unregisterMiPush");
}
}

- (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
{
NSLog(@"command error(%d|%@): %@", error, [self getOperateType:selector], data);
}

- (void)miPushReceiveNotification:(NSDictionary*)data
{
// 1.当启动长连接时, 收到消息会回调此处
// 2.[MiPushSDK handleReceiveRemoteNotification]
// 当使用此方法后会把APNs消息导入到此
NSLog(@"XMPP notify: %@", data);
[MiPushPlugin onNotificationMessageArrivedCallBack:data];
}

- (NSString*)getOperateType:(NSString*)selector
{
NSString *ret = nil;
if ([selector hasPrefix:@"registerMiPush:"] ) {
ret = @"客户端注册设备";
}else if ([selector isEqualToString:@"unregisterMiPush"]) {
ret = @"客户端设备注销";
}else if ([selector isEqualToString:@"registerApp"]) {
ret = @"注册App";
}else if ([selector isEqualToString:@"bindDeviceToken:"]) {
ret = @"绑定 PushDeviceToken";
}else if ([selector isEqualToString:@"setAlias:"]) {
ret = @"客户端设置别名";
}else if ([selector isEqualToString:@"unsetAlias:"]) {
ret = @"客户端取消别名";
}else if ([selector isEqualToString:@"subscribe:"]) {
ret = @"客户端设置主题";
}else if ([selector isEqualToString:@"unsubscribe:"]) {
ret = @"客户端取消主题";
}else if ([selector isEqualToString:@"setAccount:"]) {
ret = @"客户端设置账号";
}else if ([selector isEqualToString:@"unsetAccount:"]) {
ret = @"客户端取消账号";
}else if ([selector isEqualToString:@"openAppNotify:"]) {
ret = @"统计客户端";
}else if ([selector isEqualToString:@"getAllAliasAsync"]) {
ret = @"获取Alias设置信息";
}else if ([selector isEqualToString:@"getAllTopicAsync"]) {
ret = @"获取Topic设置信息";
}

return ret;
}

@end
35 changes: 35 additions & 0 deletions src/ios/Plugins/MiPushPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Created by wenin819 on 17-03-24.
//

#import <Cordova/CDV.h>

@interface MiPushPlugin : CDVPlugin

// 注册小米推送
-(void)init:(CDVInvokedUrlCommand*)command;
-(void)shouldInit:(CDVInvokedUrlCommand*)command;

-(void)showToast:(CDVInvokedUrlCommand*)command;

// 设置标签、别名
-(void)setAlias:(CDVInvokedUrlCommand*)command;
-(void)unSetAlias:(CDVInvokedUrlCommand*)command;

// 设置userAccount
-(void)setUserAccount:(CDVInvokedUrlCommand*)command;
-(void)unSetUserAccount:(CDVInvokedUrlCommand*)command;


// 订阅topic
-(void)setTopic:(CDVInvokedUrlCommand*)command;
-(void)unSetTopic:(CDVInvokedUrlCommand*)command;

// 接受到消息
+(void)onNotificationMessageArrivedCallBack:(NSDictionary*)data;
// 用户点击
+(void)onNotificationMessageClickedCallBack:(NSDictionary*)data;
// 小米推送注册成功
+(void)onReceiveRegisterResultCallBack:(NSString*)regId;

@end
Loading

0 comments on commit 1764749

Please sign in to comment.