-1
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;
import com.htl.jsfshoppingfestival.DataBaseHandler;
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
private NotificationManager mNotificationManager;
Context ctx;
int countNotification=0;
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("Message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
// Toast.makeText(getApplicationContext(), "Notification :"+message, Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(),from+message,Toast.LENGTH_LONG).show();
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
DataBaseHandler db = new DataBaseHandler(this);
db.addMessage(message);
countNotification=db.getRowCount();
db.close();
Log.d("sender", "Broadcasting message");
Intent localIntent = new Intent("badge count");
// You can also include some extra data.
localIntent.putExtra("Count", countNotification);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
我沒有得到消息的通知時,我發送通知,我的應用程序顯示:在通知不幸的是應用程序已經停止錯誤發送
不幸的是應用程序已經停止
在調試我得到了message=null
和應用程序給出了錯誤。
OnMessageReceived –
那麼肯定沒有得到在'Bundle',請onec有 –
http://stackoverflow.com/questions/15881033/get-string-from-bundle-android-returns-null這個鏈接可以幫助你 –