我正在尝试获得一个简单的通知。我见过很多使用旧 API 方法的示例,但我想使用较新的方法。我已经从 API 资源中将这段代码放在一起,它运行时没有错误。但是,我没有看到任何通知。我有查看通知的最低要求,至少,从我所读的内容来看,我相信是这样。这是我的代码:
public class Login extends Activity {
public static Context ctx;
public void onCreate(Bundle savedInstanceState) {
ctx = this;
}
private static void notice(String msgfrom, String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("New message from " + msgfrom.toString());
mBuilder.setContentText(msg);
mBuilder.build();
}
}
感谢下面的帮助,我修改了我的代码
NotificationCompat.Builder nb = new NotificationCompat.Builder(ctx);
nb.setSmallIcon(R.drawable.ic_launcher);
nb.setContentTitle("New message from " + msgfrom.toString());
nb.setContentText(msg);
nb.setAutoCancel(true);
Notification notification = nb.build();
NotificationManager NM = (NotificationManager)
ctx.getSystemService(NOTIFICATION_SERVICE);
NM.notify(0, notification);