通知没有被解雇

时间:2014-04-29 02:31:51

标签: android notifications

我无法点击按钮向通知栏发送通知。我的代码中似乎缺少一些要做的事情。任何帮助,将不胜感激。感谢。

代码:

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());
}

}

1 个答案:

答案 0 :(得分:0)

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

你可能错过了setSmallIcon()

创建通知


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

必填通知内容

Notification对象必须包含以下内容:

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

希望有所帮助