|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core'; |
| 3 | +import { Observable } from 'rxjs/Observable'; |
| 4 | + |
| 5 | +declare const baiduPush: any; |
| 6 | + |
| 7 | +export interface BaiduResponse<T> { |
| 8 | + /** |
| 9 | + * The corresponding Baidu SDK method called. |
| 10 | + */ |
| 11 | + type: string; |
| 12 | + /** |
| 13 | + * The error code corresponding to Baidu's request. |
| 14 | + */ |
| 15 | + errorCode?: string; |
| 16 | + /** |
| 17 | + * Registration data revelvant to subsequent actions. |
| 18 | + */ |
| 19 | + data: T; |
| 20 | +} |
| 21 | + |
| 22 | +export interface RegistrationData { |
| 23 | + /** |
| 24 | + * The ID registered to Baidu for the app. |
| 25 | + */ |
| 26 | + appId: string; |
| 27 | + /** |
| 28 | + * The ID registered to Baidu for the device. |
| 29 | + */ |
| 30 | + userId: string; |
| 31 | + /** |
| 32 | + * The channel ID registered to Baidu for the app. |
| 33 | + */ |
| 34 | + channelId: string; |
| 35 | +} |
| 36 | + |
| 37 | +export interface UnregistrationData { |
| 38 | + /** |
| 39 | + * The ID corresponding to the Baidu request. |
| 40 | + */ |
| 41 | + requestId: string; |
| 42 | +} |
| 43 | + |
| 44 | +export interface TagData { |
| 45 | + /** |
| 46 | + * The ID corresponding to the Baidu request. |
| 47 | + */ |
| 48 | + requestId: string; |
| 49 | + /** |
| 50 | + * The channel ID registered to Baidu for the app. |
| 51 | + */ |
| 52 | + channelId: string; |
| 53 | + /** |
| 54 | + * The list of successfully set/deleted tags. |
| 55 | + */ |
| 56 | + sucessTags: string[]; |
| 57 | + /** |
| 58 | + * The list of unsuccessfully set/deleted tags. |
| 59 | + */ |
| 60 | + failTags: string[]; |
| 61 | +} |
| 62 | + |
| 63 | +export interface NotificationData { |
| 64 | + /** |
| 65 | + * The title of the notification. |
| 66 | + */ |
| 67 | + title: string; |
| 68 | + /** |
| 69 | + * The description of the notification. |
| 70 | + */ |
| 71 | + description: string; |
| 72 | + /** |
| 73 | + * Custom content for the notification. |
| 74 | + */ |
| 75 | + customContentString?: string; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * @name Baidu Push |
| 80 | + * @description |
| 81 | + * This plugin faciliates the use of Baidu Push notifications. |
| 82 | + * |
| 83 | + * @usage |
| 84 | + * ```typescript |
| 85 | + * import { BaiduPush } from '@ionic-native/baidu-push'; |
| 86 | + * |
| 87 | + * |
| 88 | + * constructor(private baiduPush: BaiduPush) { } |
| 89 | + * |
| 90 | + * ... |
| 91 | + * |
| 92 | + * this.baiduPush.startWork('xxxxxx') |
| 93 | + * .then((res: any) => console.log(res)) |
| 94 | + * .catch((error: any) => console.error(error)); |
| 95 | + * |
| 96 | + * ``` |
| 97 | + * @interfaces |
| 98 | + * BaiduResponse |
| 99 | + * RegistrationData |
| 100 | + * UnregistrationData |
| 101 | + * TagData |
| 102 | + * NotificationData |
| 103 | + */ |
| 104 | +@Plugin({ |
| 105 | + pluginName: 'BaiduPush', |
| 106 | + plugin: 'cordova-plugin-push-baidu', |
| 107 | + pluginRef: 'baiduPush', |
| 108 | + repo: 'https://github.com/Ti-webdev/cordova-plugin-push-baidu.git', |
| 109 | + platforms: ['Android', 'iOS'] |
| 110 | +}) |
| 111 | +@Injectable() |
| 112 | +export class BaiduPush extends IonicNativePlugin { |
| 113 | + /** |
| 114 | + * This method registers the device to Baidu Cloud Push services. |
| 115 | + * @param {string} apiKey Baidu Cloud Push API key. |
| 116 | + * @return {Promise<BaiduResponse<RegistrationData>>} Returns a Promise that resolves with a BaiduResponse. |
| 117 | + */ |
| 118 | + @Cordova() |
| 119 | + startWork(apiKey: string): Promise<BaiduResponse<RegistrationData>> { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * This method unregisters the device to Baidu Cloud Push services. |
| 125 | + * @return {Promise<BaiduResponse<UnregistrationData>>} Returns a Promise that resolves with a BaiduResponse. |
| 126 | + */ |
| 127 | + @Cordova() |
| 128 | + stopWork(): Promise<BaiduResponse<UnregistrationData>> { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * This method re-binds the device to Baidu Cloud Push services. |
| 134 | + * @return {Promise<BaiduResponse<RegistrationData>>} Returns a Promise that resolves with a BaiduResponse. |
| 135 | + */ |
| 136 | + @Cordova() |
| 137 | + resumeWork(): Promise<BaiduResponse<RegistrationData>> { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * This sets tags in the Baidu Cloud Push services. |
| 143 | + * @param tags {any} tags The tags to set. |
| 144 | + * @return {Promise<BaiduResponse<TagData>>} Returns a Promise that resolves with a BaiduResponse. |
| 145 | + */ |
| 146 | + @Cordova() |
| 147 | + setTags(tags: any): Promise<BaiduResponse<TagData>> { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * This sets tags in the Baidu Cloud Push services. |
| 153 | + * @param tags {any} tags The tags to set. |
| 154 | + * @return {Promise<BaiduResponse<TagData>>} Returns a Promise that resolves with a BaiduResponse. |
| 155 | + */ |
| 156 | + @Cordova() |
| 157 | + delTags(tags: any): Promise<BaiduResponse<TagData>> { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * This method is called when a notification is recieved on the foreground. |
| 163 | + * @return {Promise<BaiduResponse<NotificationData>>} Returns a Promise that resolves with a BaiduResponse. |
| 164 | + */ |
| 165 | + @Cordova({ observable: true }) |
| 166 | + onMessage(): Observable<BaiduResponse<NotificationData>> { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * This method is called when the user taps a notification. |
| 172 | + * @return {Promise<BaiduResponse<NotificationData>>} Returns a Promise that resolves with a BaiduResponse. |
| 173 | + */ |
| 174 | + @Cordova({ observable: true }) |
| 175 | + onNotificationClicked(): Observable<BaiduResponse<NotificationData>> { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * This method is called when a notification is recieved. |
| 181 | + * @return {Promise<BaiduResponse<NotificationData>>} Returns a Promise that resolves with a BaiduResponse. |
| 182 | + */ |
| 183 | + @Cordova({ observable: true }) |
| 184 | + onNotificationArrived(): Observable<BaiduResponse<NotificationData>> { |
| 185 | + return; |
| 186 | + } |
| 187 | +} |
0 commit comments