收到通知时,GCM启动后台/关闭的应用程序(Android)

收到通知时,GCM启动后台/关闭的应用程序(Android)

问题描述:

我对GCM有问题. 我创建了一个服务器端通知推送程序(使用PHP),该推送程序将通知发送给具有该应用程序的特定用户(基于位置).

I have a problem with GCM. I created a server side notification pusher (in PHP), that send to specific user (based on location) who have the app, a notification.

我有三个用于管理注册和通知侦听器的类.

I have three class for managing the registration and the notification listener.

public class PusherIDListenerService extends InstanceIDListenerService {
    private static final String TAG = "PUSHER_ID_LISTENER_SERVICE";

    @Override
    public void onTokenRefresh() {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}


现在注册课程


Now the registration class

public class RegistrationIntentService extends IntentService {
    private static final String TAG = "REGISTRATION_SERVICE";

    private int idClient;

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        this.idClient = intent.getIntExtra("idClient", 0);
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        String locationProvider = LocationManager.NETWORK_PROVIDER;
        Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

        double lat = 0;
        double lon = 0;
        if (lastKnownLocation != null) {
            lat = lastKnownLocation.getLatitude();
            lon = lastKnownLocation.getLongitude();
        } else {
            Log.d(TAG, "Position not available, updating next time.");
        }

        try {
            synchronized (TAG) {
                InstanceID instanceID = InstanceID.getInstance(this);
                String senderId = getString(R.string.gcm_defaultSenderId);
                String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

                this.sendRegistrationToServer(token, lat, lon);
            }
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
        }

        Intent registrationComplete = new Intent(this, ActStartPage.class);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    private void sendRegistrationToServer(String token, double lat, double lon) {
        // doing some stuff
    }
}


现在是侦听器


And now the Listener

public class PusherListenerService extends GcmListenerService {
    private static final String TAG = "PUSHER_LISTENER_SERVICE";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String title = data.getString("title");
        String message = data.getString("body");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        sendNotification(title, message);
    }

    private void sendNotification(String title, String message) {
        Intent intent = new Intent(this, ActStartPage.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("menuTabId", 3);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.cancelAll();
        notificationManager.notify((int)(Math.random()), notificationBuilder.build());
    }
}


我的清单看起来像这样


My manifest look like this

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>
    <service
        android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.PusherListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.PusherIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <service
        android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.RegistrationIntentService"
        android:exported="false">
    </service>


最后,我在onCreate的主要活动中启动了所有功能


And finally i launch all that in my main activity in the onCreate

    if (checkPlayServices()) {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        intent.putExtra("idClient", Integer.parseInt(this.getResources().getString(R.string.clientId)));
        startService(intent);
    }


现在,什么起作用了:


So now, what is working :

  • 启动注册:确定
  • 在应用程序位于前台或后台或从当前应用程序中删除时发送通知:确定
  • 单击通知,然后在应用程序处于前台状态时再次启动该应用程序:确定

什么不起作用=(:

What's not working =( :

  • 单击通知,然后在应用程序处于后台或从当前应用程序中删除时再次启动应用程序

我尝试了一些方法,但是没有任何效果.我也不太了解xml设置.

I tried some things, but nothing worked. I don't really good understand the xml settings too.

这是我发送给gcm的JSON的示例:

Here is an example of the JSON i send to gcm :

"notification":{
    "body":"blabla",
    "title":"one title"
    "icon":"notification_icon"
},
"data":{
    "body":"blabla",
    "title":"one title"
},
"registration_ids":["id1", "id2", etc]

那么,当我在关闭应用程序时收到通知时,再次启动该应用程序时,我的代码有什么错误(或必须添加什么)?

有什么想法吗?

谢谢=)

好,

我刚刚解决了这个问题.

I just fixed the problem.

我发送的JSON就像:

The JSON i send is like :

"notification":{
    "body":"blabla",
    "title":"one title"
    "icon":"notification_icon"
},
"data":{
    "body":"blabla",
    "title":"one title"
},
"registration_ids":["id1", "id2", etc]


但是这部分(来自JSON):


But this part (from the JSON) :

"notification":{
    "body":"blabla",
    "title":"one title"
    "icon":"notification_icon"
}

是问题所在.

我认为JSON消息分为两部分,当应用程序打开时为数据",而当应用程序关闭或在后台时为通知". 我以为,由于该应用已关闭,android如何才能知道他必须显示通知?所以这就是为什么使用"notification"来指定android必须显示通知的原因.

I thought the JSONmessage is in two part, "data" when app is open, and "notification" when app is closed or in background. I thought that, because the app is closed, how can android know that he must show a notification ? So it's why use "notification" for specify that android must show a notification.

但是,不,通知"用于发送简单通知而无需采取任何措施.

But no, "notification" is used for send simple notification without action.

所以我通过发送此JSON解决了问题:

"data":{
    "body":"blabla",
    "title":"one title"
},
"registration_ids":["id1", "id2", etc]