未收到 GCM 消息
Posted
技术标签:
【中文标题】未收到 GCM 消息【英文标题】:GCM message not received 【发布时间】:2016-04-18 09:31:05 【问题描述】:我已经尝试实施 GCM 已经有一段时间了……但我被困住了……我已经在下面发布了代码和必要的详细信息。这是清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="mypackage.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
android:name="mypackage.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launch"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".screens.WebActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.RegistrationIntentService" />
<service
android:name="mypackage.service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".service.GcmMessageHandler"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<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" />
<category android:name="mypackage" />
</intent-filter>
</receiver>
</application>
</manifest>
其他相关类文件
GcmMessageHandler:
extends GcmListenerService
String message;
public static final int MESSAGE_NOTIFICATION_ID = 435345;
@Override
public void onMessageReceived(String from, Bundle data)
message = data.getString("message");
createNotification(from, message);
private void createNotification(String from, String message)
Context context = getBaseContext();
NotificationCompat.Builder mbuilder = new NotificationCompat
.Builder(context).setSmallIcon(R.mipmap.ic_launch)
.setContentTitle(from)
.setContentText(message);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mbuilder.build());
MyInstanceIDListenerService
extends InstanceIDListenerService
@Override
public void onTokenRefresh()
Intent tokenrefreshIntent = new Intent(this,RegistrationIntentService.class);
startService(tokenrefreshIntent);
RegistrationIntentService:
extends IntentService
private static final String PREFS_TOKEN_KEY = "my_token_key";
private static final String QuickstartPreferences = "QuickstartPreferences";
private static String User_EmailId;
String senderId;
String token;
SharedPreferences sharedPrefs;
NetworkOperations networkOperations;
public RegistrationIntentService(String name)
super(name);
public RegistrationIntentService()
super(PREFS_TOKEN_KEY);
@Override
protected void onHandleIntent(Intent intent)
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// initializeClasses();
// make call to the instance api//
InstanceID instanceID = InstanceID.getInstance(this);
senderId = getResources().getString(R.string.google_app_id);
//request token that will be used by the server to send the notification//
try
token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
User_EmailId = getEmiailID(getApplicationContext());
Log.d("register_Token: ", token);
sendRegistrationToServer(token, User_EmailId);
catch (IOException e)
Log.d("error_httpproblem", e.getMessage() + " & " + e.getLocalizedMessage());
e.printStackTrace();
sharedPrefs.edit().putBoolean(QuickstartPreferences, false).apply();
private void sendRegistrationToServer(String token,String email)
GcmTokenPost gcmTokenPost = new GcmTokenPost();
try
gcmTokenPost.postToken(token,email);
catch (IOException e)
Log.d("error_httpproblem", e.getMessage() + " & " + e.getLocalizedMessage());
e.printStackTrace();
sharedPrefs.edit().putString(PREFS_TOKEN_KEY, token);
sharedPrefs.edit().putBoolean(QuickstartPreferences, true).apply();
Log.d("shared_prefs", sharedPrefs.getString(PREFS_TOKEN_KEY, "default value"));
private String getEmiailID(Context context)
AccountManager my_accountManager = AccountManager.get(context);
Account account = getAccount(my_accountManager);
if (account == null)
return null;
else
return account.name;
private static Account getAccount(AccountManager accountManager)
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0)
account = accounts[0];
else
account = null;
return account;
// private void initializeClasses()
//
// networkOperations = new NetworkOperations();
//
我从 gcm 得到什么回应
我可以成功地将令牌从应用程序发送到我的服务器,然后从那里发送到 GCM 服务器..但我无法从 gcm 服务器获取任何内容。
【问题讨论】:
您的服务器可能没有向 GCM 发送数据 Khizar hayat @, florian-do@ 我认为我的服务器能够发送数据.. 请看看我在新编辑中添加的图像 你从服务器发送的json是什么,你是否在json中的“token”中添加了键值对? 看看发送到 GCM 服务器的 json去吗?","title"=>"来自我的服务器的你好!","color"=>"#03A9F4","icon" =>"@drawable/ic_back","sound" => "default","click_action " =>"OPEN_MAIN_ACTIVITY"), "data"=>array("message"=>"Hello") );我正在从 db 读取registrationIds 你在哪里定义必须发送消息的令牌? 【参考方案1】:首先我认为你应该从谷歌开发网站https://developers.google.com/cloud-messaging/android/start尝试这个教程,在你成功理解所有代码后,你可以使用这个链接Add Cloud Messaging to your app
我认为你的问题出在你的 RegistrationIntentService
类上,没有订阅令牌功能:
此代码由 google-sample github 提供
private static final String[] TOPICS = "global";
/**
* Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
*
* @param token GCM token
* @throws IOException if unable to reach the GCM PubSub service
*/
// [START subscribe_topics]
private void subscribeTopics(String token, String id) throws IOException
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS)
pubSub.subscribe(token, "/topics/" + topic, null);
// [END subscribe_topics]
【讨论】:
Khizar hayat @, florian-do@ 我认为我的服务器能够发送数据.. 请看看我在新编辑中添加的图像 尝试来自 GCM github github.com/googlesamples/google-services/tree/master/android/… 的示例服务器,您可以在 Android Sutdio./gradlew run -Pmsg="test"
的终端上使用此命令运行它,我在响应 "message_id":4633304339975809768
时收到此消息
florian-do@ 它适用于我的 oneplus 。它在某些设备上无法使用 ???可能是哪里出了问题...可能与网络或安卓版本有关???
@MohammedNathar 你的安卓显示器有问题吗?您是否正确接收来自 GCM 服务的令牌?发送消息时,您是否在两部手机上使用相同的主题?
我正确获取了令牌......并且我对所有手机都使用了相同的代码以上是关于未收到 GCM 消息的主要内容,如果未能解决你的问题,请参考以下文章