我开发了一个Android应用程序,我需要在从远程数据库读取一些数据后启动通知。
我在后台调用一个Web服务,在得到数据之后,它必须最终进行一些测试,我发起了我的通知。
在每2分钟我检查一下我的数据库中是否有新的更新,如果是这种情况,我必须发布通知。
我的后台服务实际上有我做的事情:
public class CoursesNotifications extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
final static String ACTION = "new course arriving";
final static String STOP_SERVICE_BROADCAST_KEY = "StopServiceBroadcastKey";
final static int RQS_STOP_SERVICE = 1;
//message items
final static String SERVER_URL = "localhost:8080/messages";
final static String TAG_idmessage = "idmessage";
final static String TAG_idtaxi = "idtaxi";
final static String TAG_messageaccept = "messageaccept";
final static String TAG_textmessage = "textmessage";
ArrayList<HashMap<String, String>> ticketList;
private static NotificationManager mNotificationManager;
CoursesNotifications coursesnotificationservce;
@Override
public void onCreate() {
super.onCreate();
coursesnotificationservce = new CoursesNotifications();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new MessageCourse().execute();
Log.d(ACTION,"");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// this.unregisterReceiver(coursesnotificationservce);
super.onDestroy();
// I want to restart this service again in one hour
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (1000 * 60 * 60),
PendingIntent.getService(this, 0, new Intent(this, CoursesNotifications.class), 0)
);
}
private class MessageCourse extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
if (ticketList != null) {
setNormalNotification();
}
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(SERVER_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONArray messages = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < messages.length(); i++) {
JSONObject messageobjet = messages.getJSONObject(i);
String idmessage = messageobjet.getString(TAG_idmessage);
String idtaxi = messageobjet.getString(TAG_idtaxi);
boolean messageaccept = messageobjet.getBoolean(TAG_messageaccept);
String textmessage = messageobjet.getString(TAG_textmessage);
// tmp hashmap for single contact
HashMap<String, String> message = new HashMap<String, String>();
// adding each child node to HashMap key => value
message.put(TAG_idmessage, idmessage);
message.put(TAG_idtaxi, idtaxi);
message.put(TAG_messageaccept, String.valueOf(messageaccept));
message.put(TAG_messageaccept, textmessage);
// adding contact to contact list
ticketList.add(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (ticketList != null) {
setNormalNotification();
}}
private Notification setNormalNotification() {
Bitmap remote_picture = null;
// Setup an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(getApplicationContext(), RepondreCourse.class);
// TaskStackBuilder ensures that the back button follows the recommended convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(RepondreCourse.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);
return new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.taxi)
.setAutoCancel(true)
// .setLargeIcon(remote_picture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.profile, "One", resultPendingIntent)
.addAction(R.drawable.document, "Two", resultPendingIntent)
.addAction(R.drawable.message, "Three", resultPendingIntent)
.setContentTitle("Normal Notification")
.setContentText("This is an example of a Normal Style.").build();
}
这是我的BraodcastReceiver:
public class CourseNotificationReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
{
context.startService(new Intent(context, CoursesNotifications.class));
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent gpsTrackerIntent = new Intent(context, CourseNotificationalarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, gpsTrackerIntent, 0);
SharedPreferences sharedPreferences = context.getSharedPreferences("com.taxi.binov.taxidriver.Services.prefs", Context.MODE_PRIVATE);
int intervalInMinutes = sharedPreferences.getInt("intervalInMinutes", 1);
Boolean currentlyTracking = sharedPreferences.getBoolean("currentlyTracking", false);
// if (currentlyTracking) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), intervalInMinutes * 60000*5, pendingIntent); // 3*60000 = 3 minute,
}
最后我的最后一堂课是为了完成这项任务:
public class CourseNotificationalarmReciever extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, CoursesNotifications.class)); }
但是,当我运行应用程序时,通知不会显示
我真的希望得到你的帮助
答案 0 :(得分:0)
您没有致电notify()
方法,而且原因是通知没有显示!
int notifyId = 1;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyId, mBuilder.build());