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);
}
}
当我发送通知时,我没有收到消息作为通知,我的应用显示:
不幸的是app停止了
在调试中,我得到了message=null
,而app却出错了。
答案 0 :(得分:0)
您需要检查不等于null的消息。
if(message != null && !message.trim().isEmpty())
{
sendNotification(message);
}
答案 1 :(得分:0)
你在哪个区块中空? OnMessageReceived或SendNotification?