2013-06-21 48 views
1

我已阅读网站上的许多帖子,我相信为了弹出基本通知,不需要编写许多代码行。未出现Android通知

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Log.i("StartUpActivity", "onCreate"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_start_up); 


    NotificationManager notiManager = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setContentTitle("Hello") 
     .setContentText("hello") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setAutoCancel(false); 
    notiManager.notify(0, mBuilder.build()); 
    Log.i("here", "here"); 

} 

我也试过Notification.Builder,它也不起作用。我不知道问题出在哪里。由于intent/pendingintent对通知是可选的,我只是没有添加代码。 (虽然即使我添加意图通知不显示)

我也添加权限VIBRATE在清单,我不知道的用法。

我只想知道如何在状态栏上弹出通知。谢谢。

+0

贵 “这里” 的日志来了吗? – dymmeh

+0

它似乎干净,不知道尝试将ID从0改为另一个正数,看看它是否有效。 –

+0

尝试了你的代码,因为它的工作原理非常好! –

回答

0

VIBRATE权限是允许设备振动。您没有使用此权限,您可以将其删除。

您需要在.setAutoCancel(false)后调用.build()。

0

使用sdk 14兼容性的Android(22目标sdk版本)上的最新版本,您必须在通知中添加一个“样式”以显示精简版的预览。

例如:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
bigTextStyle.setBigContentTitle(getString(R.string.notifications_title)); 
bigTextStyle.bigText(messageText); 
bigTextStyle.setSummaryText(getString(R.string.notification_call_to_action)); 
mBuilder.setStyle(bigTextStyle); 

notifyManager.notify(0, mBuilder.build());