[英]Android GCM. Application not receiving the push notification
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.. 因此,在没有任何帮助的情况下阅读了很多StackOverflow线程,并阅读了许多Google文档之后,我在这里询问是否可以找人找出问题所在。
I have app which should only register with the InstanceID token on my server and be able to get push notifications from GCM. 我有只应在服务器上使用InstanceID令牌注册的应用,并且能够从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: 在logcat中,我可以看到推送已到达设备,但尚未到达应用程序:
I/GCM﹕ GCM message com.kazav.gabi.pushtest 0:1442125487591772%7dfbbb577dfbbb57
Some of my code: 我的一些代码:
AndroidMnifest.xml: 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: 还有我的Push处理程序Java文件:
PushHendler.java: 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.. 我在onMessageRecieved函数上有断点,但我的应用程序从未使用过。
Do anyone see something wrong with my code? 有人看到我的代码有问题吗?
Thanks, Gabi. 谢谢,加比。
I dont see the GCMIntentService of your GCM implementation. 我没有看到您的GCM实现的GCMIntentService。 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. 最重要的方法是类似于generateNotification之类的方法,该方法通过
pending Intent
侦听传入的消息并生成通知。 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 . 有关其他信息,请参阅本教程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.