2016-09-16 1 views
0

J'implémentaient simplement une petite discussion app.When l'application est fermée, je ne reçois pas notifications.When est l'application est ouverte, je reçois des notificationsService Contexte Android est en cours d'exécution, mais pas obtenir des notifications

protected void onHandleIntent(Intent intent) { 

     final ResultReceiver receiver = intent.getParcelableExtra("receiver"); 
     String url = intent.getStringExtra("url"); 
     Bundle bundle = new Bundle(); 
     for(;;) { 
      if (!TextUtils.isEmpty(url)) { 
       receiver.send(STATUS_RUNNING, Bundle.EMPTY); 

       try { 
        String results = downloadData(url); 
        if (null != results) { 
         bundle.putString("result", results); 
         receiver.send(STATUS_FINISHED, bundle); 
         displayNotification(results); 
        } 
       } catch (Exception e) { 
        bundle.putString(Intent.EXTRA_TEXT, e.toString()); 
        receiver.send(STATUS_ERROR, bundle); 
       } 
      } 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

Cette est le code d'activité. le clic du bouton je commence le service et il fonctionne lorsque l'application est l'application opened.When est un service fermé ne fonctionne pas

if (bt != null) { 
      bt.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //Intent intent = new Intent(Intent.ACTION_SYNC, null, this, BgIntentService.class); 
        String abbc = Intent.ACTION_SYNC; 
        Context ctx = MainActivity.this; 
        Intent intent = new Intent(abbc, null, ctx, BgIntentService.class); 
        intent.putExtra("url", url); 
        intent.putExtra("receiver", new ResultReceiver(new Handler()) { 
         @Override 
         protected void onReceiveResult(int resultCode, Bundle resultData) { 
          switch (resultCode) { 
           case BgIntentService.STATUS_RUNNING: 
            setProgressBarIndeterminateVisibility(true); 
            break; 
           case BgIntentService.STATUS_FINISHED: 
            String abc = resultData.getString("result"); 
            // Toast.makeText(getApplicationContext(), abc, Toast.LENGTH_SHORT).show(); 
            break; 
           case BgIntentService.STATUS_ERROR: 
            String error = resultData.getString(Intent.EXTRA_TEXT); 
            Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show(); 
            break; 
          } 
         } 
        }); 
        startService(intent); 
       } 
      }); 

Voici le code de la notification d'affichage

protected void displayNotification(String msg) { 
     Log.i("Start", "notification"); 
     NotificationManager mNotificationManager; 
     int numMessages = 0; 

    /* Invoking the default notification service */ 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 

     mBuilder.setContentTitle("New Message"); 
     mBuilder.setContentText("You've received new message."); 
     mBuilder.setTicker("New Message Alert!"); 
     mBuilder.setSmallIcon(R.drawable.ic_launcher); 

    /* Increase notification number every time a new notification arrives */ 
     mBuilder.setNumber(++numMessages); 

    /* Add Big View Specific Configuration */ 
     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

     String[] events = new String[6]; 
     events[0] = msg; 


     // Sets a title for the Inbox style big view 
     inboxStyle.setBigContentTitle("Big Title Details:"); 

     // Moves events into the big view 
     for (int i=0; i < events.length; i++) { 
      inboxStyle.addLine(events[i]); 
     } 

     mBuilder.setStyle(inboxStyle); 

    /* Creates an explicit intent for an Activity in your app */ 
     Intent resultIntent = new Intent(this, NotificationView.class); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(NotificationView.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); 

     mBuilder.setContentIntent(resultPendingIntent); 
     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    /* notificationID allows you to update the notification later on. */ 
     mNotificationManager.notify(1, mBuilder.build()); 

    } 
+0

mmm ... Pourquoi ne pas utiliser notification push? – Aspicas

+0

J'espère que vous n'appelez pas stopSelf() ou stopService (..) –

+0

partagez votre code de service – Anjali

Répondre

0

Assurez-vous votre service est un service collant

Si ce n'est pas un service collant, il ne sera pas actif après la fermeture de l'application