2

当应用程序在前台/后台/被终止时,我收到推送通知,但在应用程序终止时未调用 onMessageReceived。我编写了一个代码来继续在 onMessageReceived 方法中播放自定义通知声音。当应用程序被杀死时,自定义通知声音也会播放一次。我们正在从云服务器(FCM)传递数据消息和通知消息。我很感激帮助。

MyFirebaseMessagingService.java

公共类 MyFirebaseMessagingService 扩展 FirebaseMessagingService { private static final String TAG = "MyFirebaseMessagingServ";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e("@@", ">>>>>>>>>: Inside CLASS");
    Log.e("@@", ">>>>>>>>>: Inside ON MESSAGE RECEIVED");
    Map<String, String> data = remoteMessage.getData();
    String orderID = data.get("orderId");
    String orderType = data.get("orderType");
    String orderStatus = data.get("orderStatus");
    Intent intentService = new Intent(getApplicationContext(), SoundIntentService.class);
    startService(intentService);
}
} 

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();



        // sending reg id to your server
        sendRegistrationToServer(refreshedToken);

        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
        registrationComplete.putExtra("token", refreshedToken);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    private void sendRegistrationToServer(final String token) {
        // sending gcm token to server
        Log.e(TAG, "sendRegistrationToServer: " + token);
    }


}

清单.xml

<service
            android:name=".service.SoundIntentService"
            android:exported="false" />
<service android:name=".service.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".service.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
4

0 回答 0