我正在开发带有expo和push通知的应用程序,但工作正常,但是没有声音,并且不会弹出。.
注意:它只会振动,而不会发出通知声。
我的客户端
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
答案 0 :(得分:0)
我将其与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>
);
}