应用程序终止时,推送通知不会调用OnMessageReceive

时间:2018-06-01 12:57:23

标签: android android-push-notification

我在前台/后台/已杀死的应用程序中获取推送通知但在应用程序终止时未调用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);
    }


}

的Manifest.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>

0 个答案:

没有答案