React Native,博览会通知,没有得到通知,会有什么问题?

dzjeubhm  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(37)

我正试图使用React Native,博览会通知。我想得到通知推本地从应用程序,如果它是关闭或打开我。
当你醒来的时候,
结束的时候就是你睡觉的时候。
notificationCount是您希望获取的计数。
好吧,它的工作在远离这并不是说有一个问题,但我没有得到任何通知,从这个和平的代码,我会告诉你

import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import * as Notifications from 'expo-notifications';

const NotificationScheduler = () => {
  const startTime = new Date();
  // Start time: 02:00 adjust, (hour, min, 0, 0)
  startTime.setHours(2, 0, 0, 0); 
  const endTime = new Date();
  // End time: 03:30 adjust, (hour, min, 0, 0)
  endTime.setHours(3, 30, 0, 0); 
  // Number of notifications, How many times you want to get notification.. 
  // ATTH; Higher number is just to get them more frequently while testing.
  const notificationCount = 240; 
  // This is just to get the message displayed on the screen if any, if it does, and no notification
  // is displayed, then something is working but not as it should.
  const [notificationMessage, setNotificationMessage] = useState('No notifications yet.');
  // Getting the difference on time, to calculate the frequent interval on the time. 
  const timeDifference = endTime.getTime() - startTime.getTime();
  // Making the interval.
  const interval = Math.max(1, Math.round(timeDifference / notificationCount));

  useEffect(() => {
    // Ensure I have permission to send notifiactions
    const requestPermissions = async () => {
      const { status } = await Notifications.requestPermissionsAsync();
      if (status !== 'granted') {
        // if not please go to settings and allow them
        alert('Please go to settings and allow notifications!');
        return;
      }
    };

    requestPermissions();

    // The scheduler for notification.
[2:21 AM]
const scheduleNotifications = async () => {
  const currentDate = new Date();
  console.log('------ Starts  ------')
  for (let i = 1; i < notificationCount; i++) {
    // making the trigger for sending notifications on spesific time and sqedule them.
    const trigger = new Date(startTime.getTime() + i * interval);
    console.log('trigger : ' + trigger);
    // Setting up the notififactions
    await Notifications.scheduleNotificationAsync({
      content: {
        title: "Time to check!",
        body:`This is notification number ${i + 1}.`,
        data: { data: 'data goes here'},
      },
      trigger: {
        ...trigger,
        repeats: true,
      },
    });
  }
};

const notificationListener = Notifications.addNotificationReceivedListener(notification => {
  setNotificationMessage(notification.request.content.body);
});

const foregroundNotificationListener = Notifications.addNotificationResponseReceivedListener(response => {
  setNotificationMessage(response.notification.request.content.body);
});

scheduleNotifications();

return () => {
  Notifications.removeNotificationSubscription(notificationListener);
  Notifications.removeNotificationSubscription(foregroundNotificationListener);
};
}, []);

return (
<View>
  <Text>Notifications scheduled!</Text>
  <Text>Latest Notification: {notificationMessage}</Text>
</View>
);
};

export default NotificationScheduler;

字符串

bz4sfanl

bz4sfanl1#

我自己想出来的,谢天谢地,解决办法很简单。

for (let i = 1; i <= notificationCount; i++) {
        // making the trigger for sending notifications on spesific time and sqedule them.
        const triggerHour = new Date(startTime.getTime() + i * interval).getHours();
        const triggerMin = new Date(startTime.getTime() + i * interval).getMinutes();
     
        console.log('triggerhour : ' + triggerHour + '  Triggermin : ' +triggerMin);
        // Setting up the notififactions
        const sec = i * 15;
        await Notifications.scheduleNotificationAsync({
          content: {
            title: "Time to check!",
            body:'This is notification hour : ' + triggerHour + '. og ' + triggerMin + ' min.',
            data: { data: 'data goes here'},
          },
          trigger: {
            hour: triggerHour,
            minute: triggerMin,
            repeats: true,
          },
        });
      }

字符串
`
由于我只是在每天的特定时间寻找重复,这应该可以。但我不确定我是否需要添加更多功能。我需要稍后垃圾收集通知,或者稍后将其设置为false就足够了?

相关问题