gpt4 book ai didi

android - 未从后端收到 GCM 推送通知

转载 作者:行者123 更新时间:2023-11-30 02:47:28 24 4
gpt4 key购买 nike

我正在开发一款包含推送通知的新应用。这是我到目前为止所做的:

public class C2DMMessageReceiver extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private final String TAG = "GCM Notification receiver..";
NotificationCompat.Builder builder;

public C2DMMessageReceiver() {
super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);

if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " + extras.toString());
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
C2DMRegistrationReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("עדכוני TGSpot")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}

这是处理从我的第三方服务器后端代码接收来自 GCM 的数据的代码。

设备的 token 已成功保存在服务器上,它们通过常规 HTTP Post 请求推送到服务器。

这是 Android list :

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

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

<uses-permission android:name="android.permission.INTERNET"/>
<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="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar"
android:label="@string/app_name">
<receiver
android:name=".C2DMRegistrationReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" >
</action>

<category android:name="com.tgspot.android" />
</intent-filter>
</receiver>
<service android:name=".C2DMMessageReceiver">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<activity
android:name="com.tgspot.android.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>
</application>

C2DMRegistrationReceiver.class

public class C2DMRegistrationReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Registration Receiver called");

if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
Log.w("C2DM", "Received registration ID");
final String registrationId = intent
.getStringExtra("registration_id");
String error = intent.getStringExtra("error");

Log.d("C2DM", "dmControl: registrationId = " + registrationId
+ ", error = " + error);
String deviceId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
sendRegistrationIdToServer(deviceId, registrationId);
// Also save it in the preference to be able to show it later
saveRegistrationId(context, registrationId);
} else if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
ComponentName comp = new ComponentName(context.getPackageName(),
C2DMMessageReceiver.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}

private void saveRegistrationId(Context context, String registrationId) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("C2DMM_AUTH", registrationId);
edit.commit();
}

public void sendRegistrationIdToServer(String deviceId,
String registrationId) {


Log.d("C2DM", "Sending registration ID to my application server");
final HttpClient client = new DefaultHttpClient();
final HttpPost post = new HttpPost("http://www.tgspot.co.il/register/");

// set up the POST data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// Get the deviceID
nameValuePairs.add(new BasicNameValuePair("os", "Android"));
nameValuePairs.add(new BasicNameValuePair("token",
registrationId));

// encode the POST data
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
//e1.printStackTrace();
}

// execute the HTML POST call to hit the server
new Thread(new Runnable() {
public void run() {
try {
HttpResponse response = client.execute(post);
} catch (ClientProtocolException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
}
}

最佳答案

在您的 list 中,您缺少 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
C2DMRegistrationReceiver 的 Intent 过滤器.没有它,GCM 消息广播就无法到达该应用。

您没有在问题中包含 list 的权限部分,所以我不能说您是否遗漏了其他内容。

你的 C2DMRegistrationReceiver仅处理 REGISTRATION Intent 。它还应该处理 com.google.android.c2dm.intent.RECEIVE .这些是接收 GCM 消息的 Intent 。

实际上,如果您使用 GoogleCloudMessaging.register 的新同步注册方法(我看到您已经在使用该类),C2DMRegistrationReceiver 根本不需要处理 REGISTRATION Intent 。

你的 onReceive方法应更改为:

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Registration Receiver called");

if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
Log.w("C2DM", "Received registration ID");
final String registrationId = intent
.getStringExtra("registration_id");
String error = intent.getStringExtra("error");

Log.d("C2DM", "dmControl: registrationId = " + registrationId
+ ", error = " + error);
String deviceId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
sendRegistrationIdToServer(deviceId, registrationId);
// Also save it in the preference to be able to show it later
saveRegistrationId(context, registrationId);
} else if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
ComponentName comp = new ComponentName(context.getPackageName(),
C2DMMessageReceiver.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}

这样一个com.google.android.c2dm.intent.RECEIVE广播会触发 C2DMMessageReceiver显示通知的服务。

如果您使用新的注册方法,则根本不需要第一个 if 条件。

关于android - 未从后端收到 GCM 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791861/

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