gpt4 book ai didi

android - 无法使用适用于 Android 的 Google Cloud Messaging(不是帮助程序库)接收消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:35 25 4
gpt4 key购买 nike

有没有人可以帮助我提供一个使用适用于 Android 的 Google 云消息传递从 gcm 接收消息的工作示例。我已经尝试了两种方法(帮助程序库和 GoogleCloudMessaging 类),但似乎没有任何效果。我正在使用显示以下内容的 PHP 脚本:

多播 ID:5.2108110103215E+18成功处理的消息数:1处理错误的消息数:0规范 ID:0

显然一切正常。我可以通过两种方式注册设备,使用帮助程序库 (gcm.jar) 和使用 GoogleCloudMessaging 类。问题是我通过 PHP 发送的消息无法到达,或者至少我不知道如何正确处理它。以下是我的 list 中的权限和接收者:

<permission android:name="com.example.gcm.permission.C2D_MESSAGE" 
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.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.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />

<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>

<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

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

最后是服务类

public class GCMIntentService extends GCMBaseIntentService {

private static final String PROJECT_ID = "49XXXXXXXX6";
private static final String TAG = "GCM Intent Service";

public GCMIntentService()
{
super(PROJECT_ID);
Log.d(TAG, "GCMIntentService init");
}


@Override
protected void onError(Context ctx, String sError) {

Log.d(TAG, "Error: " + sError);
}

@Override
protected void onMessage(Context ctx, Intent intent) {

Log.d(TAG, "Message Received");

String message = intent.getStringExtra("message");

sendGCMIntent(ctx, message);

}


private void sendGCMIntent(Context ctx, String message) {

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("GCM_RECEIVED_ACTION");

broadcastIntent.putExtra("gcm", message);

ctx.sendBroadcast(broadcastIntent);

}

@Override
protected void onRegistered(Context ctx, String regId) {

Log.d(TAG, regId);

// Notify main UI to update registration status
Intent registrationIntent = new Intent();
registrationIntent.setAction("registered");
registrationIntent.putExtra("regId", regId);
sendBroadcast(registrationIntent);
}

@Override
protected void onUnregistered(Context ctx, String regId) {
//...

}
}

这是使用 GoogleCloudMessaging 类时的代码(我更改了 list 以使用自定义接收器):

public class GCMBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "GCM Receiver";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private Context ctx;

@Override
public void onReceive(Context context, Intent intent) {

Log.d(TAG, "Message received");

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ intent.getExtras().toString());
}
else {
sendNotification("Received: " + intent.getExtras().toString());
}
setResultCode(Activity.RESULT_OK);
}

// Put the GCM message into a notification and post it.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);

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

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setSmallIcon(R.drawable.ic_launcher_temp)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);

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

}

事情是,一切似乎都很好,但消息永远不会到达。有任何想法吗??提前致谢。

最佳答案

在 list 中添加以下内容

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

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

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

关于android - 无法使用适用于 Android 的 Google Cloud Messaging(不是帮助程序库)接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397056/

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