2016-08-14 52 views
0

當我的服務器返回特定值時,我需要顯示通知。我有一個運行在android和taskschedule上的服務,每次向服務器發送請求時,當服務器返回正值時,我需要在celular顯示器上顯示消息,類似於whatsapp(顯示圖標和顯示通知)的接收消息。任何人都有樣品?如何在Android應用程序上顯示通知?

我想這一點:

PendingIntent resultPendingIntent = PendingIntent.getActivity(
    this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

但我的應用程序作爲服務運行。

+0

你必須建立通知任何代碼? – Shaishav

回答

0

請使用Using Service to run background and create notification

private void processStartNotification() { 
     // Do something. For example, fetch fresh data from backend to create a rich notification? 

     final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setContentTitle("Scheduled Notification") 
       .setAutoCancel(true) 
       .setColor(getResources().getColor(R.color.colorAccent)) 
       .setContentText("This notification has been triggered by Notification Service") 
       .setSmallIcon(R.drawable.notification_icon); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 
       NOTIFICATION_ID, 
       new Intent(this, NotificationActivity.class), 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     builder.setContentIntent(pendingIntent); 
     builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this)); 

     final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(NOTIFICATION_ID, builder.build()); 
    } 
相關問題