Procházet zdrojové kódy

local notification

leon před 1 rokem
rodič
revize
d6a5e18088
4 změnil soubory, kde provedl 89 přidání a 3 odebrání
  1. 28 1
      android/app/src/main/AndroidManifest.xml
  2. 1 0
      package.json
  3. 55 2
      src/pages/clock/Clock.tsx
  4. 5 0
      yarn.lock

+ 28 - 1
android/app/src/main/AndroidManifest.xml

@@ -7,7 +7,8 @@
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.VIBRATE" />
-
+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
+    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
     <queries>
       <intent>
         <action android:name="android.intent.action.DIAL" />
@@ -21,6 +22,12 @@
       android:roundIcon="@mipmap/ic_launcher_round"
       android:allowBackup="false"
       android:theme="@style/AppTheme">
+        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
+            android:value="false"/>
+        <!-- Change the resource name to your App's accent color - or any other color you want -->
+        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
+            android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->
+
         <meta-data
             android:name="JPUSH_CHANNEL"
             android:value="online" />
@@ -29,6 +36,26 @@
             android:value="7cf918ada725a9e9aecc8a17" />
         <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
             android:resource="@mipmap/ic_launcher" />
+
+
+        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
+        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
+        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
+                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
+            </intent-filter>
+        </receiver>
+
+        <service
+            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
+            android:exported="false" >
+            <intent-filter>
+                <action android:name="com.google.firebase.MESSAGING_EVENT" />
+            </intent-filter>
+        </service>
         <activity
         android:name=".MainActivity"
         android:label="@string/app_name"

+ 1 - 0
package.json

@@ -109,6 +109,7 @@
 		"react-native-onesignal": "^5.1.3",
 		"react-native-pager-view": "~6.0.1",
 		"react-native-permissions": "^4.1.5",
+		"react-native-push-notification": "^8.1.1",
 		"react-native-safe-area-context": "~4.4.1",
 		"react-native-screens": "^3.18.2",
 		"react-native-svg": "~13.7.0",

+ 55 - 2
src/pages/clock/Clock.tsx

@@ -70,6 +70,7 @@ let Linking;
 let JPush;
 let checkNotification, uploadPermissions, NativeAppEventEmitter;
 let messaging;
+let PushNotification;
 
 if (process.env.TARO_ENV == 'rn') {
     messaging = require('@react-native-firebase/messaging').default
@@ -81,6 +82,7 @@ if (process.env.TARO_ENV == 'rn') {
     useNavigation = require("@react-navigation/native").useNavigation
     checkNotification = require('@/utils/native_permission_check').checkNotification;
     uploadPermissions = require('@/utils/native_permission_check').uploadPermissions;
+    PushNotification = require('react-native-push-notification')
 }
 
 const defaultValue = `[{"scenario":{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}},"current_record":{"scenario":"FAST_SLEEP","status":"WAIT_FOR_START","fast":{"status":"WAIT_FOR_START","target_start_time":1710514800000,"target_end_time":1710576000000},"sleep":{"status":"WAIT_FOR_START","target_start_time":1710540000000,"target_end_time":1710572400000}},"wx_pub_followed":false,"time_input_schema":{"min":60,"max":1380,"step":5},"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}},{"scenarios":[{"name":"FAST","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"}}},{"name":"SLEEP","count":0,"schedule":{"sleep":{"start_time":"22:00","end_time":"07:00"}}},{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}}],"select_count":0,"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}}]`
@@ -171,6 +173,57 @@ export default function Page() {
         else {
             loadRNCache()
 
+            PushNotification.configure({
+                onRegister: function (token) {
+                    console.log('TOKEN:', token);
+                    PushNotification.localNotification({
+                        channelId: "channel-id-1",
+                        title: '推送通知',
+                        message: '这是一条本地推送通知',
+                        playSound: true,
+                        soundName: 'system',
+                    });
+
+                    PushNotification.localNotification({
+                        title: '通知栏推送',
+                        message: '这是一条通知栏推送通知',
+                        playSound: true,
+                        soundName: 'system',
+                        android: {
+                            priority: 'high',
+                        },
+                    });
+                },
+
+                onNotification: function (notification) {
+                    console.log('NOTIFICATION:', notification);
+                    // notification.finish(PushNotificationIOS.FetchResult.NoData);
+                },
+
+                permissions: {
+                    alert: true,
+                    badge: true,
+                    sound: true,
+                },
+
+                popInitialNotification: true,
+                requestPermissions: true,
+            });
+
+            PushNotification.localNotificationSchedule({
+                //... You can use all the options from localNotifications
+                message: "My Notification Message444", // (required)
+                date: new Date(Date.now() + 10 * 1000), // in 60 secs
+                allowWhileIdle: true, // (optional) set notification to work while on doze, default: false
+
+                /* Android Only Properties */
+                repeatTime: 1, // (optional) Increment of configured repeatType. Check 'Repeating Notifications' section for more info.
+            });
+
+
+
+
+
         }
 
         if (process.env.TARO_ENV == 'rn') {
@@ -220,7 +273,7 @@ export default function Page() {
             checkAuthorized();
             //检查用户是否添加过小程序
             checkAddToMini();
-            wxPubFollow({force_refresh:true});
+            wxPubFollow({ force_refresh: true });
 
 
 
@@ -527,7 +580,7 @@ export default function Page() {
 
 
     function getCheckData() {
-        
+
         Promise.all([getClocks(), getPlans()]).then((list) => {
             setErrorPage(false)
             setCheckData(list)

+ 5 - 0
yarn.lock

@@ -16847,6 +16847,11 @@ react-native-permissions@^4.1.5:
   resolved "https://registry.yarnpkg.com/react-native-permissions/-/react-native-permissions-4.1.5.tgz#db4d1ddbf076570043f4fd4168f54bb6020aec92"
   integrity sha512-r6VMRacASmtRHS+GZ+5HQCp9p9kiE+UU9magHOZCXZLTJitdTuVHWZRrb4v4oqZGU+zAp3mZhTQftuMMv+WLUg==
 
+react-native-push-notification@^8.1.1:
+  version "8.1.1"
+  resolved "https://registry.yarnpkg.com/react-native-push-notification/-/react-native-push-notification-8.1.1.tgz#a41d20c70ea5a7709417e96261b225461f8dc73a"
+  integrity sha512-XpBtG/w+a6WXTxu6l1dNYyTiHnbgnvjoc3KxPTxYkaIABRmvuJZkFxqruyGvfCw7ELAlZEAJO+dthdTabCe1XA==
+
 react-native-root-siblings@^4.0.0, react-native-root-siblings@^4.1.1:
   version "4.1.1"
   resolved "https://registry.npmmirror.com/react-native-root-siblings/-/react-native-root-siblings-4.1.1.tgz#b7742db7634a87f507eb99a5fd699c4f10c46ab0"