-1
我在android中製作一個應用程序,它應該像一個簡單的任務提醒應用程序一樣工作。
我的問題是,我希望應用程序發送通知給用戶,如果之前選擇時間到了。我正在使用BroadcastReceiver
和AlarmManager
來安排警報。我知道我必須將notification /和alarm_id設置爲唯一,但我不知道如何發送具有特定標題和文本的多個通知。所以標題應該是保存在SQL數據庫中的任務的名稱。 我試圖將變量設置爲靜態,但對我沒有幫助,因爲它會將所有通知更改爲相同的文本。 這是我的廣播接收器 - >Android使用獨特的文本準時發送通知
public class AlertReceiver extends BroadcastReceiver
{
private Context context;
@Override
public void onReceive(Context context, Intent intent)
{
this.context = context;
String title = intent.getStringExtra("Title");
createNotification("Title", "Message", "Time is up");
Log.d("AlertReceiver", "Extra: " + title);
}
private void createNotification(String title, String message, String ticker)
{
PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, new Intent(context, NavigationActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentIntent(notificationIntent);
mBuilder.setSmallIcon(R.drawable.ic_access_alarms_white_24dp);
mBuilder.setContentTitle(title);
mBuilder.setContentText(message);
mBuilder.setTicker(ticker);
mBuilder.setAutoCancel(true);
mBuilder.setDefaults(android.support.v7.app.NotificationCompat.DEFAULT_ALL);
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify((int)System.currentTimeMillis(), mBuilder.build());
}
}
希望有人能幫助我。 在此先感謝