@pluggableai/smartpush-sdk
Pluggable的Smartpush插件,用于NativeScript应用程序
npm i --save @pluggableai/smartpush-sdk

@pluggableai/smartpush-sdk

Pluggable的Smartpush插件,用于NativeScript应用程序

npm install @pluggableai/smartpush-sdk

先决条件 [与FCM集成]

在使用此插件之前,您应确保您的应用程序已与NativeScript Firebase集成,然后与Firebase Cloud Messaging (FCM)集成。步骤非常容易遵循。

  1. 通过在项目根目录中运行以下命令安装@nativescript/firebase-core插件(用于在应用程序中初始化FirebaseApp的插件)(在使用任何其他Firebase服务之前必须安装此插件):
npm install @nativescript/firebase-core
  1. Android配置 - 必须下载并添加到项目的配置文件
  • Firebase控制台中添加新的Android应用程序并输入项目详情。必须将“Android包名”与本地项目的包名匹配,该包名可以在nativescript.config.ts文件中找到;
  • 下载google-services.json文件并将其放置在项目中的以下位置:/App_Resources/Android/src/google-services.json
  1. iOS配置 - 要允许iOS应用程序安全地连接到Firebase项目,必须下载并添加到项目的配置文件
  • Firebase控制台中添加新的iOS应用程序并输入项目详情。必须将“Apple Bundle ID”与您的本地项目Bundle ID匹配。Bundle ID可以在打开项目时在Xcode的“通用”选项卡中找到或在其nativescript.config.ts文件中找到;
  • 下载GoogleService-Info.plist文件并将其放置在项目中的以下位置:/App_Resources/iOS/GoogleService-Info.plist
  • 然后,您需要执行一系列步骤以获取认证密钥.p8文件(APNs认证密钥)以向Apple设备发送推送通知。如果您还没有APNs认证密钥,请确保在Apple开发者会员中心中创建一个。然后,在Firebase控制台中选中您的项目,选择项目设置,然后选择“云消息”选项卡。在“Apple应用程序配置”下的APNs认证密钥处,点击上传按钮。浏览到您保存密钥的位置,选择它,然后点击打开。为密钥添加密钥ID(在Apple开发者会员中心中可用)并点击上传。

  1. 实例化Firebase并初始化默认应用程序(例如,在/app/app.ts文件中)
import { firebase } from '@nativescript/firebase-core'
const defaultApp = await firebase().initializeApp()
  1. 现在,您可以通过运行以下命令安装消息模块:
npm install @nativescript/firebase-messaging
  1. 然后,通过导入@nativescript/firebase-messaging模块添加SDK。您应该在应用程序中导入此模块一次,最好在主文件中(例如app.ts或main.ts)。
import '@nativescript/firebase-messaging'
  1. iOS配置 - 在您可以通过Firebase接收和发送消息之前,iOS需要进一步的配置。阅读文档并遵循如何设置带有Firebase Cloud Messaging的iOS的步骤。
  • 注意,上述教程的步骤中有一个是不完整的。特别是关于 允许处理接收到的后台推送通知 的步骤。您应该打开 app/App_Resources/iOS/Info.plist 并在底部添加以下代码
<key>UIBackgroundModes</key>
<array>
<string>processing</string>
<string>fetch</string>
<string>remote-notification</string>
</array>
  1. 然后,您可以在根目录的终端中重新构建项目
ns clean
ns build android
ns build ios
  1. 要处理本地和点击通知,您还需要 shared-notification-delegate 插件
npm install @nativescript/shared-notification-delegate

注意:通过 APNs 的 FCM 在 iOS 模拟器上 无法工作。要接收消息和通知,需要一个真实设备。对于 Android 也是如此。

Smartpush 使用

在配置您的应用程序以集成 FCM 之后,您就可以使用此插件与用户进行适当的互动了。要安装插件,只需运行

  1. 首先通过在项目根目录中运行 npm install @pluggableai/smartpush-sdk 安装 Smartpush 包

  2. 这个库公开了两个方法,可以像这样导入和使用

...
import { SmartpushSdk } from '@pluggableai/smartpush-sdk';
...

const defaultApp = async function () {
console.log('defaultApp function started');
await firebase().initializeApp();
// Allows you to always display notifications while the application is in the foreground
// without sending additional parameters/data when sending the push notification
firebase().messaging().showNotificationsWhenInForeground = true
console.log('Firebase Initialized');
}
defaultApp();
// Request user permission
async function requestUserPermission() {
const authStatus = await firebase()
.messaging()
.requestPermission({
ios: {
alert: true
}
})
const enabled =
authStatus === AuthorizationStatus.AUTHORIZED ||
authStatus === AuthorizationStatus.PROVISIONAL

console.log('Authorization status: ', authStatus)
if (enabled) {
const didRegister = await firebase().messaging().registerDeviceForRemoteMessages().then(() => {
// Init Firebase and calling the below methods
setFirebaseInits();
});
}
}

requestUserPermission()
// Register handler to listen for messages
firebase().messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived: ' + JSON.stringify(remoteMessage))
const notifData = remoteMessage.data as {[key: string]: string}
const notificationChannelId = "notificationChannelId"
const notificationChannelName = "notificationChannelName"
let pushUUID = SmartpushSdk.pluggableExecute(notificationChannelId, notificationChannelName, notifData)
console.log('[UUID]: ', pushUUID)
})
// What happens when user clicks in a received push
firebase().messaging().onNotificationTap((message) => {
console.log('CLICKED NOTIFICATION: ', message)
SmartpushSdk.pluggableStoreFeedback();
})

// Needed SharedNotificationDelegate to handle local notifications
SharedNotificationDelegate.addObserver({
userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler: (notificationCenter, notification, handler, next) => {
console.log('CLICKED NOTIFICATION:', notificationCenter);
SmartpushSdk.pluggableStoreFeedback();
next();
},
userNotificationCenterWillPresentNotificationWithCompletionHandler: (notificationCenter, notification, handler, next) => {
console.log("Notification received by observer");
handler(UNNotificationPresentationOptions.Alert);
next();
},
});
// Get user token 
firebase().messaging().getToken().then(token => {
console.log('User token: ', token)
//saveTokenToDatabase(token)
});

// Listen to user token changes
firebase().messaging().onToken(token => {
console.log('User token changed to: ', token)
//saveTokenToDatabase(token)
});
// Subscribe to specific topic
firebase().messaging().subscribeToTopic('ns_topic').then(() => {
console.log('Subscribed to topic: ns_topic')
});

注意:对于 Android,要自定义使用的通知图标,只需在 Android 的 drawable 文件夹中添加所需的图标,并将其命名为 ic_push_app_icon。否则,将使用默认图标,这与您的应用程序无关。

更多信息

  • 为了实现完全兼容性,请注意使用的 XCODE、SWIFT 和 COCOAPODS 版本。推荐版本是 XCODE=15SWIFT=5.9COCOAPODS=1.14.2

  • 有关更多信息,请访问 https://pluggableai.xyz/ 或给我们反馈 [email protected]

许可证

版权所有 © 2023 PLUGGABLE, LDA(又名 PluggableAI)

在不修改的情况下重新分发和使用源代码和二进制代码,前提是满足以下条件

  1. 源代码的分发必须保留上述版权声明、本条件列表和以下免责声明。

  2. 二进制代码的分发必须在本文档和/或其他与分发提供的材料中复制上述版权声明、本条件列表和以下免责声明。

  3. 未经事先书面许可,不得使用版权所有者的名称或其贡献者的名称来认可或推广由此软件派生出的产品。

本软件由版权所有者和贡献者提供“按原样”并且任何明示或暗示的保证,包括但不限于适销性和针对特定目的的适用性保证,均予以排除。在任何情况下,版权所有者或贡献者均不对任何直接、间接、偶然、特殊、示范性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)承担责任,无论其责任是因合同、严格责任还是侵权(包括疏忽或不计其他)而引起的,即使被告知了此类损害的可能性。