大佬教程收集整理的这篇文章主要介绍了在退出和后台状态播放推送通知的系统声音而不是自定义声音 - Android,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在推送通知时遇到了自定义声音问题。我使用:
"@react-native-community/push-notification-ios": "^1.8.0","@react-native-firebase/app": "^11.3.3","@react-native-firebase/messaging": "^11.3.3","@types/react-native-push-notification": "^7.2.0","react-native": "~0.63.4","react-native-push-notification": "^7.2.3",
现在我的情况是自定义声音仅在应用程序处于前台时播放。默认声音为后台播放和应用程序关闭状态。
localnotificationservice.ts
import PushNotification from "react-native-push-notification";
import PushNotificationIOS from "@react-native-community/push-notification-ios";
import {Platform} from "react-native";
class localnotificationservice {
configure(onopenNotification) {
PushNotification.chAnnelExists(
'alert',(exists) => {
if (!exists) {
PushNotification.createChAnnel({
chAnnelID: 'alert',chAnnelname: 'Alert',chAnnelDescription: 'Alert',vibrate: true,soundname: 'alert',},(created) => alert("Created alert " + created));
}
},);
PushNotification.getChAnnels(chAnnel_IDs => {
alert("chAnnel_IDs: " + chAnnel_IDs + " len: " + chAnnel_IDs.length )
})
PushNotification.configure({
onRegister: (token) => {
console.log("[localnotificationservice] onRegister:",token)
},onNotification: (notification) => {
if (!notification?.data) {
return;
}
notification.userInteraction = true;
onopenNotification(Platform.OS === "ios" ? notification.data.item : notification.data);
if (Platform.OS === "ios") {
notification.finish(PushNotificationIOs.FetchResult.NoData)
}
},permissions: {
alert: true,badge: true,sound: true
},popInitialNotification: true,requestPermissions: true
})
}
unregister() {
PushNotification.unregister();
}
showNotification(ID,title,message,data = {},options = {}) {
alert("options.soundname = " + options.soundName);
PushNotification.localnotification({
...this.buildAndroIDNotification(ID,data,options),...this.buildiosnotification(ID,title: title || "",message: message || "",playSound: true,soundname: Platform.OS === 'androID'
? 'alert' //androID.resource://com.rnfirebasenotification/raw/alert
: 'alert.wav',userInteraction: false,chAnnelID: "alert"
})
}
buildAndroIDNotification(ID,options = {}) {
return {
ID: ID,autoCancel: true,largeIcon: options.largeIcon || "ic_launcher",smallicon: options.smallicon || "ic_notification",bigText: message || '',subText: title || '',vibrate: options.vibrate || true,vibration: options.vibration || 300,priority: options.priority || "max",importance: options.importance || "high",data: data
}
}
buildiosnotification(ID,options = {}) {
return {
alertAction: options.alertAction || "vIEw",category: options.category || "",userInfo: {
ID: ID,item: data
}
}
}
cancelAlllocalnotifications() {
if (Platform.OS === "ios") {
PushNotificationIOs.removeAllDeliverednotifications();
} else {
PushNotification.cancelAlllocalnotifications();
}
}
removeDeliverednotificationByID(notificationID) {
console.log("[localnotificationservice] removeDeliverednotificationByID",notificationID);
PushNotification.cancellocalnotifications({ID: `${notificationID}`})
}
}
export const localnotificationservice = new localnotificationservice();
FCMservice.ts
import messaging from '@react-native-firebase/messaging'
import {Platform} from 'react-native'
class FCMservice {
messageListener: () => voID;
register(onRegister,onNotification,onopenNotification) {
this.@R_607_10943@kPermissions(onRegister);
this.createNotificationListeners(onRegister,onopenNotification);
}
async registerappWithFCM() {
if (Platform.OS === "ios") {
await messaging().registerDeviceForRemotemessages();
await messaging().setautoInitEnabled(true)
}
}
@R_607_10943@kPermissions(onRegister: (token: String) => voID) {
messaging().hasPermission()
.then(enabled => {
if (enabled) {
this.getToken(onRegister);
} else {
this.requestPermissions(onRegister)
}
})
.catch(error => {
console.log("[FCMservice] Permissions rejected ",error);
})
}
getToken(onRegister: (token: String) => voID) {
messaging().getToken()
.then(fcmToken => {
if (fcmToken) {
onRegister(fcmToken);
} else {
console.log("[FCMservice] User does not have a device token");
}
})
.catch(error => {
console.log("[FCMservice] getToken rejected ",error);
})
}
requestPermissions(onRegister: (token: String) => voID) {
messaging().requestPermission()
.then(() => {
this.getToken(onRegister)
})
.catch(error => {
console.log("[FCMservice] request Permission rejected ",error);
})
}
deletetoken() {
console.log("[FCMservice] deteletoken ")
messaging().deletetoken()
.catch(error => {
console.log("[FCMservice] delete token error",error);
})
}
createNotificationListeners(onRegister: (token: String) => voID,onopenNotification) {
// App running in BACkground
messaging()
.onNotificationopenedApp(remotemessage => {
console.log("[FCMservice] onNotificationopenedApp Notification caused app to open from BACkground state",remotemessagE)
if(remotemessagE) {
const notification = remotemessage.notification;
onopenNotification(notification);
// removeDeliverednotification(notification.notificationID);
}
})
// App is opened from a quit state
messaging()
.geTinitialNotification()
.then(remotemessage => {
console.log("[FCMservice] geTinitialNotification Notification caused app to open from quit state",remotemessagE)
if(remotemessagE) {
const notification = remotemessage.notification;
onopenNotification(notification);
// removeDeliverednotification(notification.notificationID);
}
})
// Foreground state message
this.messageListener = messaging().onmessage(async remotemessage => {
if (remotemessagE) {
let notification = null;
if(Platform.OS === "ios") {
notification = remotemessage.data.notification
} else {
notification = remotemessage.notification
}
onNotification(notification)
}
});
// triggered when have new token
messaging().onTokenrefresh(fcmToken => {
console.log("[FCMservice] New token refresh ",fcmToken)
onRegister(fcmToken);
})
}
unregister() {
this.messageListener();
}
}
export const fCMservice = new FCMservice();
App.Js
import React,{useEffect,useStatE} from 'react';
import {StyleSheet,Text,TexTinput,VIEw} from 'react-native';
import {fCMservicE} from './services/FCMservice'
import {localnotificationservicE} from "./services/localnotificationservice";
export default function App() {
const [token,setToken] = useState<String>();
useEffect(() => {
fCMservice.registerappWithFCM()
fCMservice.register(onRegister,onopenNotification)
localnotificationservice.configure(onopenNotification)
function onRegister(token) {
console.log("[App] onRegister: ",token)
setToken(token);
}
function onNotification(notify) {
console.log("[App] onNotification: ",notify)
const options = {
soundname: "fire",playSound: true
}
localnotificationservice.showNotification(
0,notify.title,notify.body,notify,options
)
}
function onopenNotification(notify) {
console.log("[App] onopenNotification: ",notify);
alert("Open Notification: " + notify.body);
}
return () => {
console.log("[App] unRegister");
fCMservice.unregister();
localnotificationservice.unregister();
}
})
return (
<VIEw style={styles.container}>
<Text>Notification token</Text>
<TexTinput value={token} />
</VIEw>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,BACkgroundcolor: '#fff',alignItems: 'center',justifyContent: 'center',});
index.Js
import { registerRootComponent } from 'expo';
import React from "react";
import messaging from '@react-native-firebase/messaging';
import App from './App';
messaging().setBACkgroundmessageHandler(async remotemessage => {
console.log("message handled in the BACkground",remotemessagE);
})
function headlesscheck({isheadless}) {
if(isheadless) {
return null;
}
return <App />;
}
// registerRootComponent calls AppRegistry.registerComponent('main',() => App);
// It also ensures that whether you load the app in the Expo clIEnt or in a native build,// the environment is set up appropriately
registerRootComponent(headlesscheck);
我尝试将 soundname 值更改为“androID.resource://com.rnfirebasenotification/raw/alert”,但随后声音消失了。
一件奇怪的事情是,当我打开应用程序时,我可以看到 2 个警报:“Created alert false”和“chAnnel_IDs: len: 0”。所以 ChAnnel 还没有被创建。第二个奇怪的事情是在模拟器上创建了频道。
我还尝试在 index.Js 的 localnotificationservice.showNotification
中执行 setBACkgroundmessageHandler
。
我在 alert.wav
中取了 androID/app/src/main/res/raw/fire.wav
的声音
我错过了背景和关闭状态的自定义声音是默认声音是什么?
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的在退出和后台状态播放推送通知的系统声音而不是自定义声音 - Android全部内容,希望文章能够帮你解决在退出和后台状态播放推送通知的系统声音而不是自定义声音 - Android所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。