gpt4 book ai didi

java - GCM :Device can send message but not registered

转载 作者:行者123 更新时间:2023-12-01 10:04:07 25 4
gpt4 key购买 nike

我目前正在制作一个基于gcm的android应用程序。当我在真实设备上运行时,它没有显示任何错误,我可以发送消息(GAE日志还显示/send语句)。问题是另一个设备没有收到任何消息。我引用了GAE日志,它没有显示任何/register语句。当我在模拟器上运行时,我会收到此错误。

04-12 14:56:38.897: E/AndroidRuntime(1181): java.lang.RuntimeException: An error occured while executing doInBackground()
04-12 14:56:38.897: E/AndroidRuntime(1181): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-12 14:56:38.897: E/AndroidRuntime(1181): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.lang.Thread.run(Thread.java:841)
04-12 14:56:38.897: E/AndroidRuntime(1181): Caused by: java.lang.NullPointerException
04-12 14:56:38.897: E/AndroidRuntime(1181): at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
04-12 14:56:38.897: E/AndroidRuntime(1181): at com.example.gcm.GcmUtil$1.doInBackground(GcmUtil.java:142)
04-12 14:56:38.897: E/AndroidRuntime(1181): at com.example.gcm.GcmUtil$1.doInBackground(GcmUtil.java:1)
04-12 14:56:38.897: E/AndroidRuntime(1181): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-12 14:56:38.897: E/AndroidRuntime(1181): ... 4 more

代码是:

  private void registerBackground() {
registrationTask = new AsyncTask<Void, Void, Boolean>() {

@Override
protected Boolean doInBackground(Void... params) {
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
//Log.d(TAG, "Attempt #" + i + " to register");
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(ctx);
}
String regid = gcm.register(Common.getSenderId());

ServerUtilities.register(Common.getPreferredEmail(),
regid);

// Save the regid - no need to register again.
setRegistrationId(regid);
return Boolean.TRUE;

} catch (IOException ex) {
//Log.e(TAG, "Failed to register on attempt " + i + ":"
+ ex);
if (i == MAX_ATTEMPTS) {
break;
}
try {
//Log.d(TAG, "Sleeping for " + backoff + " ms before
retry");
Thread.sleep(backoff);
} catch (InterruptedException e1) {
// Activity finished before we complete - exit.
//Log.d(TAG, "Thread interrupted: abort remaining
retries!");
Thread.currentThread().interrupt();
}
// increase backoff exponentially
backoff *= 2;
}
}
return Boolean.FALSE;
}

注意:我注意到 gcm.register 上的寄存器上有一条水平线...当我将鼠标悬停在它上面时,它显示 @RequiresPermission(value="com.google.android.c2dm.permission.RECEIVE")@已弃用

我已经在 android list 中提到了权限,但该行仍然没有消失。

Android list :

  <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.heylo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />

<permission
android:name="com.example.heylo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<uses-permission android:name="com.example.heylo.permission.C2D_MESSAGE" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>

<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

<application
android:name="com.example.heylo.Common"
android:allowBackup="true"
android:icon="@drawable/ic_splash"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>

<activity android:name=".SplashScreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name="com.example.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />

<category android:name="com.example.heylo" />
</intent-filter>
</receiver>

<provider
android:name="com.example.heylo.DataProvider"
android:authorities="com.example.heylo.provider"
android:exported="false" >
</provider>

<activity
android:name=".Message_List"
android:label="@string/title_activity_message__list" >
</activity>

<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>

</application>

</manifest>

最佳答案

您遵循的教程太旧了。查看 Google 开发者的消息以了解您的方式:

GCM register() is deprecated starting May 28, 2015. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens.

Source

因此建议遵循 Google 开发人员的本指南: Guide

问候

关于java - GCM :Device can send message but not registered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36581888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com