单击通知中的暂停按钮时对本机应用程序崩溃做出反应

问题描述 投票:0回答:1

我创建了一个简单的反应本机应用程序,其中我放置了一个名为“发送提醒”的按钮,当单击该按钮时,我会在 15 秒后收到通知,该通知会振动 10 秒,并在该通知中显示贪睡按钮。现在我希望当我点击小睡按钮时,通知应该关闭,振动也应该停止,5 分钟后,相同的通知再次出现,并且也会振动。一切正常,但是当我单击暂停按钮时,我的应用程序崩溃并在 logcat 上出现此错误

 Invalid ID 0x00000000.
2024-07-16 09:20:13.583 15520-15615 RNPushNotification      com.stickynotes                      W  Unable to find com.dieam.reactnativepushnotification.notification_color in manifest. Falling back to default
2024-07-16 09:20:17.949 15520-15520 RNPushNotification      com.stickynotes                      I  RNPushNotificationBootEventReceiver loading scheduled notifications
2024-07-16 09:20:17.966 15520-15520 AndroidRuntime          com.stickynotes                      D  Shutting down VM
2024-07-16 09:20:17.972 15520-15520 AndroidRuntime          com.stickynotes                      E  FATAL EXCEPTION: main
                                                                                                    Process: com.stickynotes, PID: 15520
                                                                                                    java.lang.RuntimeException: Unable to start receiver com.dieam.reactnativepushnotification.modules.RNPushNotificationActions: java.lang.SecurityException: Permission Denial: android.intent.action.CLOSE_SYSTEM_DIALOGS broadcast from com.stickynotes (pid=15520, uid=10599) requires android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS.
                                                                                                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4506)
                                                                                                        at android.app.ActivityThread.-$$Nest$mhandleReceiver(Unknown Source:0)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:204)
                                                                                                        at android.os.Looper.loop(Looper.java:291)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8134)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:601)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1019)
                                                                                                    Caused by: java.lang.SecurityException: Permission Denial: android.intent.action.CLOSE_SYSTEM_DIALOGS broadcast from com.stickynotes (pid=15520, uid=10599) requires android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS.
                                                                                                        at android.os.Parcel.createExceptionOrNull(Parcel.java:3021)
                                                                                                        at android.os.Parcel.createException(Parcel.java:3005)
                                                                                                        at android.os.Parcel.readException(Parcel.java:2988)
                                                                                                        at android.os.Parcel.readException(Parcel.java:2930)
                                                                                                        at android.app.IActivityManager$Stub$Proxy.broadcastIntentWithFeature(IActivityManager.java:5937)
                                                                                                        at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1228)
                                                                                                        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:500)
                                                                                                        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:500)
                                                                                                        at com.dieam.reactnativepushnotification.modules.RNPushNotificationActions.onReceive(RNPushNotificationActions.java:65)
                                                                                                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4497)
                                                                                                        at android.app.ActivityThread.-$$Nest$mhandleReceiver(Unknown Source:0) 
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                        at android.os.Looper.loopOnce(Looper.java:204) 
                                                                                                        at android.os.Looper.loop(Looper.java:291) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8134) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:601) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1019) 
                                                                                                    Caused by: android.os.RemoteException: Remote stack trace:
                                                                                                        at com.android.server.wm.ActivityTaskManagerService.checkCanCloseSystemDialogs(ActivityTaskManagerService.java:3398)
                                                                                                        at com.android.server.wm.ActivityTaskManagerService$LocalService.checkCanCloseSystemDialogs(ActivityTaskManagerService.java:6079)
                                                                                                        at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:15304)
                                                                                                        at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:14819)
                                                                                                        at com.android.server.am.ActivityManagerService.broadcastIntentWithFeature(ActivityManagerService.java:15777)

我的app.js代码是

import React, { useEffect } from 'react';
import { View, Text, Button, StyleSheet, Vibration } from 'react-native';
import PushNotification from 'react-native-push-notification';

const App = () => {
  useEffect(() => {
    PushNotification.configure({
      onNotification: function (notification) {
        if (notification.action === 'Snooze') {
          handleSnoozeAction();
        }
      },
    });
  }, []);

  const handleSendReminder = () => {
    // Schedule notification after 15 seconds
    setTimeout(() => {
      PushNotification.localNotification({
        channelId: 'default-channel',
        title: 'Reminder Notification',
        message: "Don't forget to take a break!",
        actions: ['Snooze'],
      });

      // Start continuous vibration for 10 seconds
      const vibrationInterval = setInterval(() => {
        Vibration.vibrate(1000); // Vibrate for 1 second
      }, 1000);

      // Stop vibration after 10 seconds
      setTimeout(() => {
        clearInterval(vibrationInterval);
        Vibration.cancel();
      }, 10000); // 10 seconds
    }, 15000); // 15 seconds
  };

  const handleSnoozeAction = () => {
    // Clear all notifications and vibrations
    PushNotification.cancelAllLocalNotifications();
    Vibration.cancel();

    // Schedule a new notification and vibration after 2 minutes
    setTimeout(() => {
      PushNotification.localNotification({
        channelId: 'default-channel',
        title: 'Reminder Notification',
        message: "Don't forget to take a break!",
        actions: ['Snooze'],
      });

      // Start continuous vibration for 10 seconds
      const vibrationInterval = setInterval(() => {
        Vibration.vibrate(1000); // Vibrate for 1 second
      }, 1000);

      // Stop vibration after 10 seconds
      setTimeout(() => {
        clearInterval(vibrationInterval);
        Vibration.cancel();
      }, 10000); // 10 seconds
    }, 120000); // 2 minutes
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Welcome to My App</Text>
      <Button title="Set Reminder" onPress={handleSendReminder} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  title: {
    fontSize: 24,
    marginBottom: 20,
  },
});

export default App;
react-native react-native-notifications
1个回答
0
投票

崩溃是由于 AndroidManifest.xml 中缺少权限造成的,您需要将

android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS
权限添加到 AndroidManifest.xml 文件中。

androidManifest.xml 位于:

android/app/src/main/

在清单标记中添加以下内容:

<uses-permission android:name="android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS"/>

完成所有这些后,重建您的应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.