Baidu push notifications plugin for NativeScript
Baidu is an alternative solution of Google FCM in China. This plugin will add Baidu push notification (http://push.baidu.com).
Prerequisites / Requirements
For getting API key follow: http://push.baidu.com/doc/guide/join
For iOS need to follow 第七章 iOS证书指导
from http://push.baidu.com/doc/ios/api to setup Baidu.
Note: I am not an expert of neigher iOS nor Android. So, please contribute if you think something you can do better :)
Installation
tns plugin add nativescript-baidu-push-notifications
Usage
Your application ID is important here. Make sure that your Baidu API key & Application ID is correct.
Import
TS/Angular:
;;
JavaScript:
pushPlugin = ;
Android
If you want to test in emulator then use Genymotion otherwise Baidu will send error message. Better to test with a real device.
let opt: AndroidOptions = apiKey: 'My API Key' icon: "res://simple_notification_icon" // optional App_Resouces/Android/drawable pushPlugin; pushPlugin; pushPlugin; pushPlugin;
iOS
iOS will require a real device. In simulator baidu will send error message.
First of all need to add this config in App_Resource/iOS/Info.plist
file:
Development Environment:
<key>insBPushAPIKey</key><string>Your-Baidu-Key</string><key>isDevBPushEnvironment</key><true/>
Production Environment:
<key>insBPushAPIKey</key><string>Your-Baidu-Key</string><key>isDevBPushEnvironment</key><false/>
JS code:
// check details https://github.com/NativeScript/push-plugin#using-the-plugin-in-ios let notificationSettings: IosRegistrationOptions = badge: true sound: true alert: true clearBadge: true interactiveSettings: actions: identifier: 'READ_IDENTIFIER' title: 'Read' activationMode: "foreground" destructive: false authenticationRequired: true identifier: 'CANCEL_IDENTIFIER' title: 'Cancel' activationMode: "foreground" destructive: true authenticationRequired: true categories: identifier: 'READ_CATEGORY' actionsForDefaultContext: 'READ_IDENTIFIER' 'CANCEL_IDENTIFIER' actionsForMinimalContext: 'READ_IDENTIFIER' 'CANCEL_IDENTIFIER' { console; }; pushPlugin; pushPlugin;
Please check demo project for more details.
All Methods/Options
; void;} // Android: void;export declare function androidUnregister(onSuccessCallback: any, onErrorCallback: any): void;export declare function onMessageReceived(callback: any): void;export declare function onNotificationArrived(callback: any): void;export declare function onNotificationClicked(callback: any): void; // iOSexport declare function iosRegister(settings: IosRegistrationOptions, success: (token: any) => void, error: (NSError: any) => void): void;export declare function registerUserNotificationSettings(success: () => void, error: (error: NSError) => void): void;export declare function iosUnregister(success: (result: any) => void, error: (error: NSError) => void): void;export declare function areNotificationsEnabled(done: (areEnabled: Boolean) => void): void;
Tips:
- For Android push notification icon you will need to add icon sets in
App_Resources/Android/src/main/res
. Better to use 36X36 dimension for icon. Check the demo. - For message notification can use
nativescript-local-notifications
plugin.
Credit
Most of the work of this plugin has been followed/copied from this libaries:
https://github.com/NativeScript/push-plugin
https://www.npmjs.com/package/nativescript-baidu-push-ins
https://www.npmjs.com/package/nativescript-baidu-push
Special thanks to Phuc Bui
and Quang Le Hong
author of above 2 npm packages.
License
Apache License Version 2.0, January 2004