如何接收来自GCM android的通知?
我正在开发一个应用程序,因为我必须从服务器接收通知。我正在成功地将注册id发送到我的服务器,但我无法从gcm服务器获取通知。我正在获取nullpointerexception。请告诉我我出错了 我的宽带接收机 GCMinentService 例外情况如何接收来自GCM android的通知?,android,google-cloud-messaging,Android,Google Cloud Messaging,我正在开发一个应用程序,因为我必须从服务器接收通知。我正在成功地将注册id发送到我的服务器,但我无法从gcm服务器获取通知。我正在获取nullpointerexception。请告诉我我出错了 我的宽带接收机 GCMinentService 例外情况 02-21 12:10:10.679: E/AndroidRuntime(30200): FATAL EXCEPTION: IntentService[GcmIntentService] 02-21 12:10:10.679: E/AndroidR
02-21 12:10:10.679: E/AndroidRuntime(30200): FATAL EXCEPTION: IntentService[GcmIntentService]
02-21 12:10:10.679: E/AndroidRuntime(30200): java.lang.NullPointerException
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.app.PendingIntent.getActivity(PendingIntent.java:191)
02-21 12:10:10.679: E/AndroidRuntime(30200): at com.technowellServices.locationfind.GcmIntentService.sendNotification(GcmIntentService.java:74)
02-21 12:10:10.679: E/AndroidRuntime(30200): at com.technowellServices.locationfind.GcmIntentService.onHandleIntent(GcmIntentService.java:62)
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.os.Handler.dispatchMessage(Handler.java:99)
您的服务文件在哪里,示例php文件我只写了这些类引用此站点我收到空指针异常我编辑我的代码查看我的日志catinside广播接收器您需要调用挂起的intent not intent,请参阅上面的共享链接他用简单的方式解释它。。。。
public class GcmIntentService extends IntentService{
Context context;
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
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(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(context, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
public class ReceiveActivity extends Activity {
TextView name;
TextView deal;
TextView valid;
TextView address;
JSONObject json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
Intent intent = getIntent();
name = (TextView) findViewById(R.id.name);
deal = (TextView) findViewById(R.id.deal);
valid = (TextView) findViewById(R.id.valid);
address = (TextView)findViewById(R.id.address);
String message = intent.getExtras().getString("message");
try {
json = new JSONObject(message);
String stime = json.getString("name");
name.setText(stime);
String slecturename = json.getString("deal");
deal.setText(slecturename);
String sroom = json.getString("valid");
valid.setText(sroom);
String sfaculty = json.getString("address");
address.setText(sfaculty);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log cat
02-21 12:10:10.679: E/AndroidRuntime(30200): FATAL EXCEPTION: IntentService[GcmIntentService]
02-21 12:10:10.679: E/AndroidRuntime(30200): java.lang.NullPointerException
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.app.PendingIntent.getActivity(PendingIntent.java:191)
02-21 12:10:10.679: E/AndroidRuntime(30200): at com.technowellServices.locationfind.GcmIntentService.sendNotification(GcmIntentService.java:74)
02-21 12:10:10.679: E/AndroidRuntime(30200): at com.technowellServices.locationfind.GcmIntentService.onHandleIntent(GcmIntentService.java:62)
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.os.Handler.dispatchMessage(Handler.java:99)