2015-05-25 75 views
-3

我開發了一個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

不,你的解釋不清楚!更具體的問題和分享更多的細節,你的嘗試,你的搜索等! –

+0

感謝您的回覆,我想做的事情是:在每2分鐘內檢查一次我的數據庫中是否有新更新,如果是這種情況,我必須發出通知。此應用程序專爲出租車司機設計:如果有新的比賽,必須通知出租車司機; – lotfi

回答

0

你是不是叫notify()方法,這就是理由通知是沒有得到顯示!

int notifyId = 1; 
NotificationManager mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(notifyId, mBuilder.build());