npm i --save nativescript-baidu-push-notifications
- 版本:1.0.6
- GitHub:
- NPM: https://npmjs.net.cn/package/nativescript-baidu-push-notifications
- 下载
- 昨天:0
- 上周:2
- 上个月:18
为 NativeScript 提供的百度推送通知插件
百度是 Google FCM 在中国的替代方案。此插件将添加百度推送通知(http://push.baidu.com)。
先决条件/要求
要获取 API 密钥,请遵循:http://push.baidu.com/doc/guide/join
对于 iOS,需要遵循 http://push.baidu.com/doc/ios/api 中的 第七章 iOS证书指导
来设置百度。
注意:我不是 iOS 或 Android 的专家。所以,如果你认为你可以做得更好,请贡献力量 :)
安装
tns plugin add nativescript-baidu-push-notifications
使用
在此处,您的应用程序 ID 很重要。请确保您的百度 API 密钥和应用程序 ID 正确。
导入
TS/Angular
import { IosRegistrationOptions, AndroidOptions } from "nativescript-baidu-push-notifications";
import * as pushPlugin from "nativescript-baidu-push-notifications";
JavaScript
pushPlugin = require("nativescript-baidu-push-notifications");
Android
如果您想在模拟器中测试,请使用 Genymotion,否则百度将发送错误消息。最好使用真实设备进行测试。
let opt: AndroidOptions = {
apiKey: 'My API Key',
icon: "res://simple_notification_icon" // optional App_Resouces/Android/drawable
}
pushPlugin.androidRegister(opt, function (data) {
console.log("Got register");
console.log("userId: " + data.get("userId"));
console.log("channelId: " + data.get("channelId"));
console.log("appid: " + data.get("appid"));
console.log("requestId: " + data.get("requestId"));
console.log("errorCode: " + data.get("errorCode"));
}, function (err) {
console.log("not register");
console.dir(err)
});
pushPlugin.onMessageReceived(function (msg, customString) {
console.log("got message")
console.log(msg);
console.log(customString);
});
pushPlugin.onNotificationClicked(function (title, msg, customString) {
console.log("clicked message")
console.log(title);
console.log(msg);
console.log(customString)
});
pushPlugin.onNotificationArrived(function (title, msg, customString) {
console.log("onNotificationArrived")
console.log(title);
console.log(msg);
console.log(customString)
});
iOS
iOS 将需要真实设备。在模拟器中,百度将发送错误消息。
首先需要将此配置添加到 App_Resource/iOS/Info.plist
文件中
开发环境
<key>insBPushAPIKey</key>
<string>Your-Baidu-Key</string>
<key>isDevBPushEnvironment</key>
<true/>
生产环境
<key>insBPushAPIKey</key>
<string>Your-Baidu-Key</string>
<key>isDevBPushEnvironment</key>
<false/>
JS 代码
// 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']
}]
},
notificationCallbackIOS: function (message) {
console.log("notificationCallbackIOS : " + JSON.stringify(message));
alert(message.alert)
}
};
pushPlugin.iosRegister(notificationSettings,
//success callback
function (result: any) {
//Register the interactive settings
if (notificationSettings.interactiveSettings) {
pushPlugin.registerUserNotificationSettings(function () {
console.log("SUCCESSFULLY REGISTER BAIDU PUSH NOTIFICATION")
console.dir(result);
}, function (err) {
console.log("ERROR REGISTER PUSH NOTIFICATION: " + JSON.stringify(err));
})
}
},
//error callback
function (error) {
console.log("REGISTER PUSH NOTIFICATION FAILED:");
console.dir(error);
}
);
pushPlugin.areNotificationsEnabled(function (areEnabled) {
console.log("Are Notifications enabled:" + JSON.stringify(areEnabled));
});
请查看示例项目以获取更多详细信息。
所有方法/选项
export interface IosInteractiveNotificationAction {
identifier: string;
title: string;
activationMode?: string;
destructive?: boolean;
authenticationRequired?: boolean;
behavior?: string;
}
export interface IosInteractiveNotificationCategory {
identifier: string;
actionsForDefaultContext: string[];
actionsForMinimalContext: string[];
}
export interface IosRegistrationOptions {
badge: boolean;
sound: boolean;
alert: boolean;
clearBadge: boolean;
interactiveSettings: {
actions: IosInteractiveNotificationAction[];
categories: IosInteractiveNotificationCategory[];
};
notificationCallbackIOS: (message: any) => void;
}
export interface NSError {
code: number;
domain: string;
userInfo: any;
}
export interface AndroidOptions {
apiKey: string;
icon?: string;
}
// Android
export declare function androidRegister(options: AndroidOptions, successCallback: any, errorCallback: any): 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;
// iOS
export 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;
提示
- 对于 Android 推送通知图标,您需要在
App_Resources/Android/src/main/res
中添加图标集。最好使用 36X36 尺寸的图标。请查看示例。 - 对于消息通知,可以使用
nativescript-local-notifications
插件。
致谢
此插件的绝大部分工作都遵循/复制自以下库
https://github.com/NativeScript/push-plugin
https://npmjs.net.cn/package/nativescript-baidu-push-ins
https://npmjs.net.cn/package/nativescript-baidu-push
特别感谢 Phuc Bui
和 Quang Le Hong
,他们是上述两个 npm 包的作者。
许可证
Apache 许可证版本 2.0,2004 年 1 月