Android 在带有GCM的设备上接收消息
实际上,我正在为Android应用程序编写通知。 我的两个主要信息来源是this和android开发者网站 让我快速描述一下我的应用程序:Android 在带有GCM的设备上接收消息,android,notifications,google-cloud-messaging,Android,Notifications,Google Cloud Messaging,实际上,我正在为Android应用程序编写通知。 我的两个主要信息来源是this和android开发者网站 让我快速描述一下我的应用程序: 在我的应用程序中,我使用web服务(WS),它与POST-HTTP请求一起工作 对于通知,我使用GCM系统 我的应用程序使用上面的“POST HTTP”请求系统在我的服务器上注册 实际上,我的服务器的工作与教程中的完全相同(这意味着在数据库中注册一个帐户,使用两个参数和设备的注册ID,并使用GCM向设备发送一条消息) 实际上,所有这些步骤都有效: My
- 在我的应用程序中,我使用
(WS),它与web服务
请求一起工作POST-HTTP
- 对于通知,我使用
系统GCM
- 我的应用程序使用上面的“POST HTTP”请求系统在我的服务器上注册
- 实际上,我的服务器的工作与教程中的完全相同(这意味着在数据库中注册一个帐户,使用两个参数和设备的注册ID,并使用GCM向设备发送一条消息)
gcm.register(发送者ID);
work){“多播id”:6276079906208554309,“成功”:1,“失败”:0,“规范id”:0,“结果”:[{“消息id”:“0:1374826298092960%978fee92f9fd7ecd”}
- 我的设备上没有收到任何信息
- 首先,我使用教程中的一部分代码制作了一个应用程序,但是使用了
,而不是包com.google.android.gms.gcm.GoogleCloudMessaging
,后者已被弃用(并在教程中使用),但我无法收到消息,所以我尝试了第二个版本com.google.android.gcm
- 这次我学习了整个教程,只是将服务器上的注册功能更改为使用我的,但与第一个应用一样,我的设备上没有收到任何内容。(我这样做是想了解接待处是如何工作的,但这对我没有帮助,现在我将以这种方式忘记)
- 我的主要活动使用android开发者网站上描述的代码
- 我使用android开发者网站上的代码创建了一个广播类
- 我没有创建任何IntentService或服务,也许这是我的错误,但根据我阅读的内容,我明白我不需要,就像之前使用不推荐的GCMBaseIntentService一样
- 我真的很想得到一些帮助来理解我需要什么来在我的设备上接收信息,因为实际上我不知道在哪里可以找到使用这个系统所需的信息
清单
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.pushnotifications2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.androidhive.pushnotifications2.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.androidhive.pushnotifications2.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Main activity. -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Register Activity -->
<activity
android:name="com.androidhive.pushnotifications2.cop.RegisterActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main Activity -->
<activity
android:name="com.androidhive.pushnotifications2.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
</activity>
<receiver
android:name=".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.androidhive.pushnotifications2" />
</intent-filter>
</receiver>
</application>
</manifest>
你能发布你的Android清单吗?请发布你正在使用的
GcmBroadcastReceiver
类。
package com.androidhive.pushnotifications2;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "GCMDemo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
///
WakeLocker.acquire(context);
///
System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
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());
}
WakeLocker.release();
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.common_signin_btn_icon_focus_light)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}