我使用GCM
进行通知
但这个班不起作用
的Manifest.xml
<service
android:name=".MyGCMListenerService"
android:exported="false"
android:enabled="true" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
MyGCMListenerService.java
public class MyGCMListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.i("error", "From: " + from);
Log.i("error", "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
错误日志:
05-02 23:11:04.894: E/AndroidRuntime(13626): FATAL EXCEPTION: AsyncTask #2
05-02 23:11:04.894: E/AndroidRuntime(13626): java.lang.NullPointerException
05-02 23:11:04.894: E/AndroidRuntime(13626): at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
05-02 23:11:04.894: E/AndroidRuntime(13626): at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
05-02 23:11:04.894: E/AndroidRuntime(13626): at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
05-02 23:11:04.894: E/AndroidRuntime(13626): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-02 23:11:04.894: E/AndroidRuntime(13626): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-02 23:11:04.894: E/AndroidRuntime(13626): at java.lang.Thread.run(Thread.java:841)