下面是我的接收器工作正常..接收功能正在被击中..我通过使用吐司确认了这一点。但为什么通知也不起作用。
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle extra = intent.getExtras();
String msg= extra.getString("message");
//Toast.makeText(getApplicationContext(), "Message received."+ msg,
// Toast.LENGTH_LONG).show();
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
RegisterActivity.this)
.setContentTitle("Notification From GCM")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mNotificationManager.notify(100, mBuilder.build());
}
};
我错过了什么吗?
答案 0 :(得分:1)
引自here:
至少,Builder对象必须包含以下内容:
- 由
设置的小图标setSmallIcon()
- 标题,由
设置setContentTitle()
- 详细信息文本,由
设置setContentText()
所以我想,您必须在通知构建器中添加一个图标。
如您所见[{3}},将无法显示带有无效图标的通知。
答案 1 :(得分:0)
您错过了setSmallIcon
,因此修改了代码
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
RegisterActivity.this)
.setContentTitle("Notification From GCM")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
到
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
RegisterActivity.this)
.setContentTitle("Notification From GCM")
.setSmallIcon(R.drawable.ic_stat_gcm)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
有关详细信息,请参阅Implementing GCM Client并搜索 sendNotification 方法。