so after reading a lot of StackOverflow threads on this, with no help, and reading a lot of Google documentation, I am asking here to see if I can get someone to figure out what's wrong..
I have app which should only register with the InstanceID token on my server and be able to get push notifications from GCM.
I am able to get the token and register on my server, but I am not getting the notification to the app.
In the logcat I can see the push is arriving to the device, but not into the app:
I/GCM﹕ GCM message com.kazav.gabi.pushtest 0:1442125487591772%7dfbbb577dfbbb57
Some of my code:
AndroidMnifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kazav.gabi.pushtest" >
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission android:name="com.kazav.gabi.pushtest.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.kazav.gabi.pushtest.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.kazav.gabi.pushtest" />
</intent-filter>
</receiver>
<service
android:name="com.kazav.gabi.pushtest.TokenReload"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="com.kazav.gabi.pushtest.PushHendler"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECIEVE" />
</intent-filter>
</service>
</application>
</manifest>
And my Push handler java file:
PushHendler.java:
package com.kazav.gabi.pushtest;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;
public class PushHendler extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
super.onMessageReceived(from, data);
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
Log.w("Notification", message);
}
}
I have breakpoint on the onMessageRecieved function but I the app is never going into it..
Do anyone see something wrong with my code?
Thanks, Gabi.
I dont see the GCMIntentService of your GCM implementation. You can have a code that acts as a service and use in the Manifest file.
The most important method is something like generateNotification that listens for the incoming message through pending Intent
and generates a notification. Make sure you add that.
Take a look at this code snippet:
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
For additional information take a look at this tutorial .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.