2014-10-12 100 views
0

下面是我的接收器,工作正常..在接收功能正在打击..我通过使用吐司证实了这一点。但为什么通知也没有工作。为什么notifation没有显示?

@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

你错过了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的方法。

+0

为什么我需要待定的意图?我不想为通知设置任何操作..我只想显示通知 – 2014-10-12 09:47:33

+1

@ Mr.Vicky尝试添加'.setSmallIcon(R.drawable.ic_stat_gcm)'并更改'ic_stat_gcm'与您的图标名称。请参阅编辑回答。 – 2014-10-12 09:48:25

+0

是的,它现在工作 – 2014-10-12 09:56:18

1
here

引用:

在最低限度,一个生成器对象必须包括以下内容:

  • 一个小图标,通过setSmallIcon()
  • 甲标题组,由setContentTitle()
  • 设置
  • 明细文本,由setContentText()设置

所以我想,你必须在你的通知生成器中放置一个图标。

正如您所见here,带有无效图标的通知将不会显示。

+0

非常感谢你..我知道.. .. – 2014-10-12 09:55:58