2014-04-29 73 views
0

我无法通过单击按钮触发通知栏的通知。有些要做的事似乎从我的代码中遗漏了。任何帮助,将不胜感激。谢谢。通知未被触发

代码:

public class MainActivity extends Activity { private int numMessages = 0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

public void onClickNotify(View view){ 


    final int notificationID = 100; 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    mBuilder.setContentTitle("Notification Alert, Click Me!"); 
    mBuilder.setContentText("Hi, This is Android Notification Detail!"); 
    mBuilder.setTicker("New Message Alert!"); 

    mBuilder.setNumber(++numMessages); 

    Intent resultIntent = new Intent(this, NotificationView.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(NotificationView.class); 

    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
       0, 
       PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     // notificationID allows you to update the notification later on. 
     mNotificationManager.notify(notificationID, mBuilder.build()); 
} 

}

回答

0

根据机器人 http://developer.android.com/guide/topics/ui/notifiers/notifications.html

你可能会错过setSmallIcon()

创建通知


您可以在NotificationCompat.Builder对象中为通知指定UI信息和操作。要创建通知本身,请调用NotificationCompat.Builder.build(),它会返回一个包含您的规范的通知对象。要发出通知,您需要通过调用NotificationManager.notify()将Notification对象传递给系统。

要求的通报内容

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

• A small icon, set by setSmallIcon() 
• A title, set by setContentTitle() 
• Detail text, set by setContentText() 

希望它有助于

+0

添加小图标帮助。感谢您的输入。 – user2191175