Reputation: 321
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>
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 =( :
I tried some things, but nothing worked. I don't really good understand the xml settings too.
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]
So what is wrong in my code (or what must i add) for launching the app again when i receive a notification when the app is closed ?
Any ideas ?
Thanks =)
Upvotes: 1
Views: 470
Reputation: 321
OK,
I just fixed the problem.
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]
But this part (from the JSON) :
"notification":{
"body":"blabla",
"title":"one title"
"icon":"notification_icon"
}
was the problem.
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.
So i resolve the problem by sending this JSON :
"data":{
"body":"blabla",
"title":"one title"
},
"registration_ids":["id1", "id2", etc]
Upvotes: 1