android - 如何在 Android 或 iPhone 上以 React Native 方式处理其他应用程序通知?

标签 android ios react-native notifications notificationlistenerservice

是否可以从我的 iPhone(或 Android)上的其他应用程序获取通知并在我的应用程序中处理它们?我是否需要用我的母语(Swift 或 Kotlin)编写这个应用程序,或者我可以使用 React Native 来实现它吗?

最佳答案

对于 Android,您可以使用 NotificationListenerService .

示例:

import { AppRegistry } from 'react-native'
import RNAndroidNotificationListener, { RNAndroidNotificationListenerHeadlessJsName } from 'react-native-android-notification-listener';

// To check if the user has permission
const status = await RNAndroidNotificationListener.getPermissionStatus()
console.log(status) // Result can be 'authorized', 'denied' or 'unknown'

// To open the Android settings so the user can enable it
RNAndroidNotificationListener.requestPermission()

/**
 * Note that this method MUST return a Promise.
 * Is that why I'm using an async function here.
 */
const headlessNotificationListener = async ({ notification }) => {/**
     * This notification is a JSON string in the follow format:
     *  {
     *      "time": string,
     *      "app": string,
     *      "title": string,
     *      "titleBig": string,
     *      "text": string,
     *      "subText": string,
     *      "summaryText": string,
     *      "bigText": string,
     *      "audioContentsURI": string,
     *      "imageBackgroundURI": string,
     *      "extraInfoText": string,
     *      "groupedMessages": Array<Object> [
     *          {
     *              "title": string,
     *              "text": string
     *          }
     *      ],
     *      "icon": string (base64),
     *      "image": string (base64), // WARNING! THIS MAY NOT WORK FOR SOME APPLICATIONS SUCH TELEGRAM AND WHATSAPP
     *  }
     * 
     * Note that these properties depend on the sender configuration so many times a lot of them will be empty
     */
    
    if (notification) {
        /**
         * You could store the notifications in an external API.
         * I'm using AsyncStorage in the example project.
         */
        
        ...
    }
}

/**
 * This should be required early in the sequence
 * to make sure the JS execution environment is setup before other
 * modules are required.
 * 
 * Your entry file (index.js) would be the better place for it.
 * 
 * PS: I'm using here the constant RNAndroidNotificationListenerHeadlessJsName to ensure
 *     that I register the headless with the right name
 */
AppRegistry.registerHeadlessTask(RNAndroidNotificationListenerHeadlessJsName,   () => headlessNotificationListener)

Apple 安全地传输通知。另外,通知通过加密仅与一个应用程序绑定(bind),这意味着无法拦截通知

关于android - 如何在 Android 或 iPhone 上以 React Native 方式处理其他应用程序通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77547456/

相关文章:

java - ContextWrapper.getFilesDir() 出现 NullPointerException 的原因

Android 文件提供程序非法参数异常

ios - 调整 iAd 横幅 View 大小

javascript - 如何将对象迭代到平面列表中 - react native ?

reactjs - 如何使用 React Hooks 在第一次渲染时加载带有 props 的子组件

android - 如何修复警告 okhttp 和 samsung spass sdk?

android - 切换按钮,禁用更改状态 onClick

reactjs - React Native 中 Java 单例的等价物是什么?

ios - 模块名称前缀应该已被 native 端剥离,但不适用于 RCTJSCExecutor

ios - 使用 indexPath 访问数组内的 UIImage 并创建空的 UIImage 数组元素