Android提醒通知

时间:2014-05-22 10:16:51

标签: android notifications

java / android相当新,但慢慢变好。我正在尝试创建一次性通知,以便在首次运行后24小时提醒用户该应用程序。我在线研究并使用stackoverflow,我终于能够使用以下通知代码运行代码:

private void  addNotification() {

NotificationCompat.Builder builder =
     new NotificationCompat.Builder(this)
     .setSmallIcon(R.drawable.ic_launcher)
     .setContentTitle("Notifications Example")
     .setContentText("This is a test notification");

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
                 PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);

// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());

}

我正在尝试调用通知功能,但它无法正常工作。我的意思如下:

addNotification();

我如何从这里开始?谢谢。所以我还没有处理24小时问题,只是试图让通知出现。然后点击它,再次启动应用程序。任何指导都表示赞赏。

1 个答案:

答案 0 :(得分:1)

试试这个:

您需要将两个参数传递给addNotification(Context context, String Message)

addNotification(this,"My Notification");

private void addNotification(Context context, String message) {

  int icon = R.drawable.ic_launcher;
  long when = System.currentTimeMillis();
  String appname = context.getResources().getString(R.string.app_name);
  NotificationManager notificationManager = (NotificationManager) context
   .getSystemService(Context.NOTIFICATION_SERVICE);

  Notification notification;
  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
  new Intent(context, myactivity.class), 0);


  NotificationCompat.Builder builder = new NotificationCompat.Builder(
  context);
  notification = builder.setContentIntent(contentIntent)
 .setSmallIcon(icon).setTicker(appname).setWhen(0)
 .setAutoCancel(true).setContentTitle(appname)
 .setContentText(message).build();

  notificationManager.notify(0 , notification);

  }

如需提醒,您可以实施 AlarmManager 计时器

检查以下示例: