Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@ohmi/push-notification-ios

Package Overview
Dependencies
Maintainers
5
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ohmi/push-notification-ios

React Native Push Notification API for iOS

latest
npmnpm
Version
1.11.1-1.2.4
Version published
Maintainers
5
Created
Source

模板版本:v0.3.0

@ohmi/push-notification-ios

Supported platforms License

本项目基于 @react-native-community/push-notification-ios@1.11.0 开发。

Gitee地址

1. 安装与使用

进入到工程目录并输入以下命令:

npm

npm install @ohmi/push-notification-ios

yarn

yarn add @ohmi/push-notification-ios

下面的代码展示了这个库的基本使用场景:

[!WARNING] 使用时 import 的库名不变。

import React, { useState } from "react";
import { View, Text, Button } from "react-native";

import PushNotification from "@react-native-community/push-notification-ios";

export const App = () => {
  const [data, setData] = useState({});

  const presentLocalNotification = () => {
    PushNotification.presentLocalNotification({
      id: "test",
      title: "title",
      subtitle: "subtitle",
      body: "body",
      repeats: true,
    });
  };

  const addSilentNotificationRequest = () => {
    PushNotification.presentLocalNotification({
      id: "test-4",
      title: "title",
      subtitle: "subtitle",
      body: "body",
      isSilent: true,
      repeats: true,
      userInfo: {
        data: "123456",
      },
    });
  };

  const addMultipleRequests = () => {
    PushNotification.presentLocalNotification({
      id: "test-1",
      title: "First",
      subtitle: "subtitle",
      body: "First Notification out of 3",
      repeats: true,
    });

    PushNotification.presentLocalNotification({
      id: "test-2",
      title: "Second",
      subtitle: "subtitle",
      body: "Second Notification out of 3",
      repeats: true,
    });

    PushNotification.presentLocalNotification({
      id: "test-3",
      title: "Third",
      subtitle: "subtitle",
      body: "Third Notification out of 3",
      repeats: true,
    });
  };

  const removeAllDeliveredNotifications = () => {
    PushNotification.removeAllDeliveredNotifications();
  };

  const removeDeliveredNotifications = () => {
    PushNotification.removeDeliveredNotifications(["test-1", "test-2"]);
  };

  const getDeliveredNotifications = () => {
    PushNotification.getDeliveredNotifications((data) => {
      if (data) {
        setData(data);
      } else {
        console.log("failed");
      }
    });
  };

  return (
    <View style={{ flexDirection: "column", justifyContent: "space-between" }}>
      <Button
        title="Add Notification Request"
        onPress={presentLocalNotification}
      />
      <Button
        title="Add Slient Notification Request"
        onPress={addSilentNotificationRequest}
      />
      <Button
        title="Add Multiple Notification Requests"
        onPress={addMultipleRequests}
      />
      <Button
        title="Set app's icon badge to 42"
        onPress={() => PushNotification.setApplicationIconBadgeNumber(42)}
      />
      <Button
        title="Clear app's icon badge"
        onPress={() => PushNotification.setApplicationIconBadgeNumber(0)}
      />
      <Button
        title="Remove All Delivered Notification Requests"
        onPress={removeAllDeliveredNotifications}
      />
      <Button
        title="Remove Delivered Notification Requests"
        onPress={removeDeliveredNotifications}
      />
      <View>
        <Button
          title="Get Delivered Notification"
          onPress={getDeliveredNotifications}
        />
        <Text>{JSON.stringify(data)}</Text>
      </View>
    </View>
  );
};

此步骤为手动配置原生依赖项的指导。

首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony

2.1. Overrides RN SDK

为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 oh-package.json5 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。

关于该字段的作用请阅读官方说明

{
  "overrides": {
    "@rnoh/react-native-openharmony": "^0.72.38" // ohpm 在线版本
    // "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
    // "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
  }
}

2.2. 引入原生端代码

目前有两种方法:

  • 通过 har 包引入;
  • 直接链接源码。

方法一:通过 har 包引入(推荐)

[!TIP] har 包位于三方库安装路径的 harmony 文件夹下。

打开 entry/oh-package.json5,添加以下依赖

"dependencies": {
    "@ohmi/push-notification-ios": "file:../../node_modules/@ohmi/push-notification-ios/harmony/push_notification.har"
  }

点击右上角的 sync 按钮

或者在命令行终端执行:

cd entry
ohpm install

方法二:直接链接源码

[!TIP] 如需使用直接链接源码,请参考直接链接源码说明

2.3. 在 ArkTs 侧引入 PushNotificationPackage

打开 entry/src/main/ets/RNPackagesFactory.ts,添加:

  ...
+ import { PushNotificationPackage } from '@ohmi/push-notification-ios/ts';

export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
  return [
    new SamplePackage(ctx),
+   new PushNotificationPackage(ctx)
  ];
}

2.4. 运行

点击右上角的 sync 按钮

或者在终端执行:

cd entry
ohpm install

然后编译、运行即可。

3. 约束与限制

3.1. 兼容性

请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:@ohmi/push-notification-ios Releases

4. API

[!TIP] "Platform"列表示该属性在原三方库上支持的平台。

[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。

NameDescriptionTypeRequiredPlatformHarmonyOS Support
presentLocalNotificationSchedules the localNotification for immediate presentation.functionnoiOSyes
scheduleLocalNotificationSchedules the localNotification for future presentationfunctionnoiOSyes
cancelAllLocalNotificationsCancels all scheduled localNotificationsfunctionnoiOSyes
addEventListenerAttaches a listener to remote or local notification events while the app is running in the foreground or the background.functionnoiOSno
removeEventListenerRemoves the event listener. Do this in componentWillUnmount to prevent memory leaksfunctionnoiOSno
addNotificationRequestSends notificationRequest to notification center at specified firedate. Fires immediately if firedate is not set.functionnoiOSyes
getDeliveredNotificationsProvides you with a list of the app’s notifications that are still displayed in Notification CenterfunctionnoiOSyes
removeAllDeliveredNotificationsRemove all delivered notifications from Notification CenterfunctionnoiOSyes
removeDeliveredNotificationsRemoves the specified delivered notifications from Notification CenterfunctionnoiOSyes
setApplicationIconBadgeNumberSets the badge number for the app icon on the home screenfunctionnoiOSyes
getApplicationIconBadgeNumberGets the current badge number for the app icon on the home screenfunctionnoiOSno
cancelLocalNotificationsCancel local notificationsfunctionnoiOSno
requestPermissionsRequests notification permissions from iOS, prompting the user's dialog box. By default, it will request all notification permissions, but a subset of these can be requested by passing a map of requested permissions. The following permissions are supportedfunctionnoiOSyes
abandonPermissionsUnregister for all remote notifications received via Apple Push Notification servicefunctionnoiOSno
checkPermissionsSee what push permissions are currently enabledfunctionnoiOSyes
getInitialNotificationThis method returns a promise. If the app was launched by a push notification, this promise resolves to an object of type PushNotificationIOS. Otherwise, it resolves to null.functionnoiOSno
getScheduledLocalNotificationsGets the local notifications that are currently scheduledfunctionnoiOSno

属性

[!TIP] "Platform"列表示该属性在原三方库上支持的平台。

[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。

NameDescriptionTypeRequiredPlatformHarmonyOS Support
idIdentifier of the notificationstringnoAllyes
titleA short description of the reason for the notificationstringyesAllyes
subtitleA secondary description of the reason for the notificationstringnoAllno
bodyThe message displayed in the notificatiostringyesAllyes
badgeThe number to display as the app's icon badgenumbernoAllyes
fireDateThe date and time when the system should deliver the notificationobjectnoAllyes
repeatsSets notification to repeatbooleannoAllno
repeatsComponentAn object indicating which parts of fireDate should be repeatedobjectnoAllno
soundThe sound played when the notification is firedstringnoAllno
categoryThe category of this notification, required for actionable notificationsstringnoAllno
isSilentIf true, the notification will appear without soundbooleannoAllyes
isCriticalIf true, the notification sound be played even when the device is locked, muted, or has Do Not Disturb enabledbooleannoAllno
criticalSoundVolumeA number between 0 and 1 for volume of critical notification. Default volume will be used if not specifiednumbernoAllno
userInfoAn object containing additional notification dataobjectnoAllyes
isTimeZoneAgnosticIf true, fireDate adjusted automatically upon time zone changes (e.g. for an alarm clock)booleannoAllno
interruptionLevelA string specifying the interruption level. Valid values are 'active', 'passive', 'timeSensitive', or 'critical'stringnoAllno

5. 遗留问题

  • HarmonyOS 的 NotificationManager 的规格和 IOS 不一致,其 NotificationRequest 所含参数,在 HarmonyOS 上部分没有适配对应参数,问题: issue#1
  • 原库部分接口在 HarmonyOS 中没有对应接口处理相关逻辑,问题: issue#2

6. 其他

7. 开源协议

本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。

Keywords

react-native

FAQs

Package last updated on 20 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts