2016-02-28 93 views
0

我有關於創建方法的通知中的代碼,但我想要在應用程序關閉時顯示通知。比如像Messenger一樣,當你連接到wifi甚至沒有打開Messenger通知即將到來。Android:當應用程序關閉時顯示通知

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_take_photo_or_upload); 

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Image"); 
    query.orderByDescending("createdAt"); 
    query.whereEqualTo("recipientUsername", ParseUser.getCurrentUser().getUsername()); 

    query.findInBackground(new FindCallback<ParseObject>() { 
     public void done(List<ParseObject> images, ParseException e) { 

      if (e == null) { 

       if (images.size() > 0) { 

        for (final ParseObject image : images) { 

         ParseFile file = image.getParseFile("image"); 
         byte[] byteArray = new byte[0]; 
         try { 
          byteArray = file.getData(); 
          bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
         } catch (ParseException e1) { 
          e1.printStackTrace(); 
         } 

         PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, new Intent(getApplicationContext(), ViewPhotos.class), 0); 
         Resources r = getResources(); 
         Notification notification = new NotificationCompat.Builder(getApplicationContext()) 
           .setLargeIcon(bitmap) 
           .setTicker("You have new photo incoming!") 
           //.setSmallIcon(R.drawable.ic_stat) 
           .setSmallIcon(R.drawable.logopt) 
           .setContentTitle("PhotoTSFun") 
           .setContentText(image.getString("sendUsername") + " has sent you an image") 
           .setContentIntent(pi) 
           .setAutoCancel(true) 
           .build(); 

         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
         notificationManager.notify(0, notification); 

        } 

       } 
      } 
     } 
    }); 

}

感謝。

+0

在服務中實現代碼並使服務在後臺連續運行。 –

回答

0

1)您必須檢測當你的應用程序中的活動應用程序類關閉 一)對破壞或方法的onStop B)onterminate。

2)從那裏意圖與出版通知在onstartcommand/onhandleintent 放代碼發送到服務

3)在服務。

相關問題