【问题标题】:Notification not getting fired通知没有被解雇
【发布时间】:2014-04-29 02:31:50
【问题描述】:

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

代码:

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

}

【问题讨论】:

    标签: android notifications


    【解决方案1】:

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

    你可能错过了 setSmallIcon()

    创建通知


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

    需要的通知内容

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

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

    希望对你有帮助

    【讨论】:

    • 添加小图标有帮助。感谢您的意见。
    猜你喜欢
    • 2012-09-18
    • 2020-03-28
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多