Android通知问题

Android通知问题,android,android-notifications,android-notification-bar,Android,Android Notifications,Android Notification Bar,我是android新手,所以请耐心等待 最后,我通过GCM从一个简单的WCF REST API获得了XML结果,希望用它的负载构建一个简单的通知 @Override protected void onMessage(Context arg0, Intent arg1) { String message = arg1.getStringExtra("payload"); NotificationManager notificationManager = (Notification

我是android新手,所以请耐心等待

最后,我通过GCM从一个简单的WCF REST API获得了XML结果,希望用它的负载构建一个简单的通知

@Override
protected void onMessage(Context arg0, Intent arg1) {

    String message = arg1.getStringExtra("payload");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Notification note = new Notification();
    note.tickerText=message;
    note.when=System.currentTimeMillis();
    note.defaults |= Notification.DEFAULT_ALL;

    notificationManager.notify(new Random().nextInt(), note);

}
我听到默认通知声音,但我的酒吧没有显示任何内容,我缺少什么


编辑I

注:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" /> 
公共通知生成()
在API级别16中添加
组合所有已设置的选项并返回新的 通知对象


编辑II

我尝试了关于使用NotificationCompat builder的建议

@Override
protected void onMessage(Context arg0, Intent arg1) {

    String Message = arg1.getStringExtra("payload");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(
            new Random().nextInt(),
            new NotificationCompat.Builder(this).setContentTitle("Message")
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setContentText(Message).build());

}

没有错误,但没有任何更改,我仍然听到声音,但没有出现,但是消息到达了onMessage方法。

Android需要设置一个小图标,以便它实际显示您的状态栏通知

从中,以下是要求:

通知对象必须包含以下内容:

•一个小图标,由setSmallIcon()设置

•标题,由setContentTitle()设置

•详细文本,由setContentText()设置

此外,较旧的平台(姜饼和更低版本)需要
pendingent
(在文档中称为内容
Intent
)来传递
通知
。这是平台的限制

根据您显示
通知的方式,通过不设置内容
意图
,应用程序将崩溃,堆栈跟踪将非常清楚原因


使用
NotificationCompat.Builder
,通过设置内容
Intent
很容易。您需要使用通知的
Builder
构建内容。这里的更多信息:@Jeeter我试图构建它,但它说最低API必须是16,但是我的Galaxy Note设备是4.0.4,它是API 15,这很奇怪,因为当你访问上面的站点,并将其设置为API级别11时,仍然允许使用Notification.Builder。@Ahmedghonem我喜欢你使用的edit I,II,等:)回到主题:我想你还需要为通知设置一个小图标。有关源示例,请参见。还有,这是这个类的文档,供参考。@A--C谢谢,Ops!这是关于添加一个小图标!当我添加它时,它工作了!谢谢,请将其添加为答案:)我没有设置contentIntent(),因为我不需要它。。我只想在单击通知后取消通知。那么NotificationCompact不会在GB上运行吗?@Mitesh我建议您提出另一个问题-我对这个问题的回答似乎与您的问题无关。如果应用程序崩溃,请确保包含任何堆栈跟踪。它与当前问题相关。通知不会出现在GB上,但在JB中有效。没有撞车问题。我已经包括了你在回答中指出的三个要点。@Mitesh我仍然建议提出一个新的问题,你需要提供代码,但在注释中格式不好。我在我的应用程序中使用了
NotificationCompat.Builder
,从2.2版开始运行良好。
@Override
protected void onMessage(Context arg0, Intent arg1) {

    String Message = arg1.getStringExtra("payload");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(
            new Random().nextInt(),
            new NotificationCompat.Builder(this).setContentTitle("Message")
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setContentText(Message).build());

}