【问题标题】:Expo push notification sound and popup doesn't work世博会推送通知声音和弹出窗口不起作用
【发布时间】:2020-08-25 00:51:46
【问题描述】:

我正在开发一个带有 expo 和推送通知的应用程序工作正常,但没有声音并且它不会弹出..

注意:它只会振动,没有提示音。

我的客户端

 if (Constants.isDevice) {
      const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
      let finalStatus = existingStatus;
      if (existingStatus !== 'granted') {
        const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
        finalStatus = status;
      }
      if (finalStatus !== 'granted') {
        alert('Failed to get push token for push notification!');
        return;
      }
      token = await Notifications.getExpoPushTokenAsync();
      console.log(token);
      this.setState({ expoPushToken: token });
    } else {
      alert('Must use physical device for Push Notifications');
    }

    if (Platform.OS === 'android') {
      Notifications.createChannelAndroidAsync('notification-sound-channel', {
        name: 'Notification Sound Channel',
        sound: true,
        priority: 'max',
        vibrate: [0, 250, 250, 250],
      });
    }

我的服务器端是php laravel:https://github.com/Alymosul/exponent-server-sdk-php

$notification = [
    'title' => 'test title',
    'body' => 'test body'
    'channelId' => 'notification-sound-channel',
];

我还使用 expo 推送通知工具对其进行了测试:https://expo.io/notifications,它的工作原理相同(振动没有声音或弹出)

环境

博览会:“^37.0.8”,

SDK 版本:27,

测试设备android版本:9

【问题讨论】:

    标签: android react-native push-notification expo


    【解决方案1】:

    我将它与核心 PHP 一起使用,它正在工作(带有声音、振动和弹出窗口)。

    这是在点击按钮时触发的 m PHP 代码。

        require_once 'vendor/autoload.php';
        $channelName = 'userdId'; // from database
        $recipient= 'ExponentPushToken[lHS_FNBss5OD-XT-rGgRLE]';
    
        // You can quickly bootup an expo instance
        $expo = \ExponentPhpSDK\Expo::normalSetup();
    
        // Subscribe the recipient to the server
        $expo->subscribe($channelName, $recipient);
    
        // Build the notification data
        $notification = ['title' => "Notification Title", 'body' => 'This is a testing notification.', 'channelId' => 'notification-sound-channel',];
    
        // Notify an interest with a notification
        $expo->notify([$channelName], $notification);
    
    

    这是我的 React 原生代码

    import React, { useEffect, useState } from "react";
    import { StyleSheet, Text, View, Button, Platform } from "react-native";
    import { Notifications } from "expo";
    import * as Permissions from "expo-permissions";
    export default function App() {
      const [expoPushToken, setExpoPushToken] = useState("");
      useEffect(() => {
        this.getPushNotificationPermissions();
      });
      getPushNotificationPermissions = async () => {
        const { status: existingStatus } = await Permissions.getAsync(
          Permissions.NOTIFICATIONS
        );
        let finalStatus = existingStatus;
        if (existingStatus !== "granted") {
          const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
          finalStatus = status;
        }
    
        if (finalStatus !== "granted") {
          return;
        }
    
        const tok = await Notifications.getExpoPushTokenAsync();
        setExpoPushToken(tok);
      };
      if (Platform.OS === "android") {
        Notifications.createChannelAndroidAsync("notification-sound-channel", {
          name: "Notification Sound Channel",
          sound: true,
          priority: "max",
          vibrate: [0, 250, 250, 250],
        });
      }
      return (
        <View>
          <Text>PHP Notification Testing</Text>
        </View>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2023-04-03
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多