应用关闭时的Android推送通知

时间:2016-02-19 10:18:41

标签: android google-cloud-messaging

我按照谷歌的文档来实施推送通知。 当应用程序打开时工作正常,但当我关闭应用程序时不会收到通知。

这是代码: 清单

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.notifiche_push_sender_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            Log.i(TAG, "GCM Registration Token: " + token);
            sendRegistrationToServer(token);
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
        }
    }

    private void sendRegistrationToServer(String token) {
        OperazioneInvioToken op = new OperazioneInvioToken(token);
        WebServiceTask ws = new WebServiceTask(op, this, false);
        ws.execute();
    }

}

RegistrationIntentService.java

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {

        Bundle notification = data.getBundle("notification");

        String title = "";
        String message = "";

        if(notification!=null){
            title = notification.getString("title");
            message = notification.getString("body");
        }

        NotificationUtility.showNotification(title, message);
        NotificationUtility.setBadge(1);
    }
}

MyGcmListenerService.java

public class MyInstanceIDListenerService extends InstanceIDListenerService {

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. This call is initiated by the
     * InstanceID provider.
     */
    @Override
    public void onTokenRefresh() {
        // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

MyInstanceIDListenerService.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(), MyGcmListenerService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmBroadcastReceiver.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        layout="@layout/toolbar_actionbar" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dp"
        android:paddingRight="6dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_search_text"
            local:MvxBind="Text SearchText" />
        <MvxListView
            android:id="@+id/category_list"
            android:orientation="vertical"
            android:choiceMode="singleChoice"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            local:MvxItemTemplate="@layout/listitem_category"
            local:MvxBind="ItemsSource Categories; SelectedItem SelectedCategory" />
    </LinearLayout>
</LinearLayout>

当应用程序打开onMessageReceived时,会调用GcmListenerService类,但不会调用应用程序关闭。

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

我通过更新com.google.android.gms:play-services:8.3.0到com.google.android.gms:play-services:8.4.0

的播放服务来解决