Android FCM推送通知未打开应用程序
我收到FCM推送通知,但应用程序处于后台时,在系统托盘中发送的通知被录制时,应用程序未打开 我的负载从后端看如下所示:Android FCM推送通知未打开应用程序,android,firebase,push-notification,firebase-cloud-messaging,payload,Android,Firebase,Push Notification,Firebase Cloud Messaging,Payload,我收到FCM推送通知,但应用程序处于后台时,在系统托盘中发送的通知被录制时,应用程序未打开 我的负载从后端看如下所示: Array( [registration_ids] => Array ([0] => some value) [priority] => high [notification] => Array ( [body] => Booking Cancelled: Your
Array(
[registration_ids] => Array
([0] => some value)
[priority] => high
[notification] => Array
(
[body] => Booking Cancelled: Your recent Booking attempt for Sound mix was declined.
[title] => Booking Rejected
[click_action] => USER_BOOKING_REJECTED
[sound] => default
)
[data] => Array
(
[type] => USER_BOOKING_REJECTED
[booking_id] => 331
)
)
我将此用户\u预订\u拒绝添加为相关活动的清单中的操作
像这样:
<activity
android:name=".view.activity.HomeActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="USER_BOOKING_APPROVED" />
<action android:name="USER_BOOKING_REJECTED" />
<action android:name="USER_BOOKING_AUTO_REJECTED" />
<action android:name="USER_CANCEL_BOOKING" />
<action android:name="USER_BOOKING_REMINDER" />
<action android:name="USER_INVOICE_EDIT" />
<action android:name="USER_INVOICE_NEW" />
<action android:name="USER_INVOICE_REFUND" />
<action android:name="USER_INVOICE_REMOVE" />
<action android:name="NEW_MESSAGE" />
<action android:name="SP_BOOKING_REMINDER" />
<action android:name="SP_BOOKING_NEW" />
<action android:name="SP_BOOKING_CANCEL" />
<action android:name="SP_INVOICE_PAYMENT" />
<action android:name="SP_NO_FUTURE_SLOT" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="@string/host"
android:pathPrefix="@string/home_path"
android:scheme="http" />
</intent-filter>
</activity>
即使我在系统托盘中收到通知,在录制通知时应用程序也不会打开。有什么帮助吗?您需要在AndroidManifest.xml中为单独的
创建单独的
s
尝试添加以下内容:
<intent-filter>
<action android:name="USER_BOOKING_REJECTED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
您需要从后端发送有效负载中的数据,而不是通知。然后调用firebase消息服务
const payload = {
data: {
/*set reuired data over here which you want for generating notification like below*/
message : `${messageSnapshot}`,
vibrate : '1',
sound : '1',
largeIcon : 'large_icon',
smallIcon : 'small_icon'
}
};
return admin.messaging().sendToDevice(deviceToken, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', deviceToken, error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(deviceToken);
}
}
});
});
<service
android:name=".services.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG = "MessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("MyFirebase","vcdjvbdbv");
if (remoteMessage == null)
return;
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Map<String, String> data = remoteMessage.getData();
String message = data.get("message");}}}
const有效载荷={
数据:{
/*在此处设置您想要生成通知的reuired数据,如下所示*/
消息:`${messageSnapshot}`,
振动:“1”,
声音:“1”,
大图标:“大图标”,
小图标:“小图标”
}
};
返回admin.messaging().sendToDevice(deviceToken,有效负载)。然后(响应=>{
//对于每条消息,检查是否有错误。
常量tokensToRemove=[];
response.results.forEach((结果,索引)=>{
常量错误=result.error;
如果(错误){
console.error('Failure sending notification to',deviceToken,error);
//清理不再注册的令牌。
if(error.code==='消息传递/无效注册令牌'||
error.code===“未注册消息传递/注册令牌”){
tokensToRemove.push(deviceToken);
}
}
});
});
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
公共静态字符串TAG=“MessagingService”;
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
Log.e(“MyFirebase”、“vcdjvbdbv”);
if(remoteMessage==null)
返回;
//检查消息是否包含数据有效负载。
如果(remoteMessage.getData().size()>0){
Map data=remoteMessage.getData();
String message=data.get(“message”);}
必须升级库才能最终工作。
整整两天我都在挠头哈哈。
大家好
implementation 'com.google.firebase:firebase-messaging:20.2.3'
你的通知点击代码在哪里?这应该会打开HomeActivity,对吗?我不知道,但我没有发现你的manifest.xml codeclick_操作有问题。据我所知,应该打开应用程序。等着知道的人。非常感谢。为操作添加了一个单独的意图过滤器,并在其中添加了类别默认值。它成功了。谢谢。来自@Anton的另一个答案帮助我解决了这个问题。