FCM 推送通知未打开应用程序
FCM Push notification is not opening the application
我正在接收 FCM 推送通知,但在应用程序处于后台时点击系统托盘中传送的通知时应用程序未打开。
我的 Payload 从后端看起来是这样的:
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
)
)
我将此 USER_BOOKING_REJECTED 添加为有关 Activity.
的清单中的操作
像这样:
<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 中的单独 <action>
创建单独的 <intent-filter>
。
尝试只添加这个:
<intent-filter>
<action android:name="USER_BOOKING_REJECTED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
您需要从后端而不是 notification.And 发送负载中的数据,然后调用 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");}}}
必须升级库才能最终运行。
整整两天都在挠我的头哈哈。
祝大家编程愉快!
implementation 'com.google.firebase:firebase-messaging:20.2.3'
我正在接收 FCM 推送通知,但在应用程序处于后台时点击系统托盘中传送的通知时应用程序未打开。
我的 Payload 从后端看起来是这样的:
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
)
)
我将此 USER_BOOKING_REJECTED 添加为有关 Activity.
的清单中的操作像这样:
<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 中的单独 <action>
创建单独的 <intent-filter>
。
尝试只添加这个:
<intent-filter>
<action android:name="USER_BOOKING_REJECTED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
您需要从后端而不是 notification.And 发送负载中的数据,然后调用 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");}}}
必须升级库才能最终运行。 整整两天都在挠我的头哈哈。 祝大家编程愉快!
implementation 'com.google.firebase:firebase-messaging:20.2.3'