2016-06-01 27 views
6

Ho provato a utilizzare Google GCM ma quando l'app è chiusa (scorri o cancella dal task manager), non riceverà alcuna notifica push. E quando apro di nuovo l'app, la notifica è già andata persa.Ricevi notifiche push Android anche se l'app è chiusa

GCM sta lavorando per: - App è aperta - App è ridotto al minimo

Non funziona: - App è chiusa (scorri da un task manager) - App è chiusa tramite applicazioni chiaro tutti aperti nel task manager

Desidero ricevere le notifiche push anche se l'app è chiusa come Facebook o Instagram. Come posso raggiungere questo obiettivo?? È possibile in GCM? se sì come? se no allora quali sono gli altri modi per raggiungere questo?

Ecco il mio codice:

AndroidManifest.xml

<!-- [START gcm_receiver] --> 
    <receiver 
     android:name="com.google.android.gms.gcm.GcmReceiver" 
     android:exported="true" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.example.airwyntin.notificationtest" /> 
     </intent-filter> 
    </receiver> 
    <!-- [END gcm_receiver] --> 

    <!-- [START gcm_listener] --> 
    <service 
     android:name=".MyGcmListenerService" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <!-- [END gcm_listener] --> 
    <!-- [START instanceId_listener] --> 
    <service 
     android:name=".MyInstanceIDListenerService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID"/> 
     </intent-filter> 
    </service> 
    <!-- [END instanceId_listener] --> 
    <service 
     android:name=".RegistrationIntentService" 
     android:exported="false"> 
    </service> 

MyGcmListenerService.java:

public class MyGcmListenerService extends GcmListenerService { 


private static int notifId = 0; 

private static final String TAG = "MyGcmListenerService"; 

/** 
* Called when message is received. 
* 
* @param from SenderID of the sender. 
* @param data Data bundle containing message data as key/value pairs. 
*    For Set of keys use data.keySet(). 
*/ 
// [START receive_message] 
@Override 
public void onMessageReceived(String from, Bundle data) { 
    String message = data.getString("alert"); 


    Log.i(TAG, "From: " + from); 

    if (message != null) { 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Message: " + message); 

     if (from.startsWith("/topics/")) { 
      // message received from some topic. 
     } else { 
      // normal downstream message. 
     } 

     // [START_EXCLUDE] 
     /** 
     * Production applications would usually process the message here. 
     * Eg: - Syncing with server. 
     *  - Store message in local database. 
     *  - Update UI. 
     */ 

     /** 
     * In some cases it may be useful to show a notification indicating to the user 
     * that a message was received. 
     */ 
     sendNotification(message); 
     // [END_EXCLUDE] 
    } 

} 
// [END receive_message] 

/** 
* Create and show a simple notification containing the received GCM message. 
* 
* @param message GCM message received. 
*/ 
private void sendNotification(String message) { 
    Intent intent = new Intent(this, NotificationView.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("GCM Tesst Message") 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    //Vibration 
    notificationBuilder.setVibrate(new long[] { 0, 200, 200, 200, 200, 200 }); 


    //LED 
    //notificationBuilder.setLights(Color.RED, 3000, 3000); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notif = notificationBuilder.build(); 
    notif.flags |= Notification.FLAG_AUTO_CANCEL; 

    /*notif.ledARGB = 0xFFff0000; 
    notif.flags = Notification.FLAG_SHOW_LIGHTS; 
    notif.ledOnMS = 100; 
    notif.ledOffMS = 100;*/ 

    notificationManager.notify(notifId++ /* ID of notification */, notif); 

} 
} 

MyInstanceIDListenerService.java:

public class MyInstanceIDListenerService extends InstanceIDListenerService { 

private static final String TAG = "MyInstanceIDLS"; 

/** 
* Called if InstanceID token is updated. This may occur if the security of 
* the previous token had been compromised. This call is initiated by the 
* InstanceID provider. 
*/ 
// [START refresh_token] 
@Override 
public void onTokenRefresh() { 
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable). 
    Intent intent = new Intent(this, RegistrationIntentService.class); 
    startService(intent); 
} 
// [END refresh_token] 


} 

RegistrationInte ntService.java:

public class RegistrationIntentService extends IntentService { 

private static final String TAG = "RegIntentService"; 
private static final String[] TOPICS = {"global"}; 

public RegistrationIntentService() { 
    super(TAG); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

    try { 
     // [START register_for_gcm] 
     // Initially this call goes out to the network to retrieve the token, subsequent calls 
     // are local. 
     // R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json. 
     // See https://developers.google.com/cloud-messaging/android/start for details on this file. 
     // [START get_token] 
     InstanceID instanceID = InstanceID.getInstance(this); 
     String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), 
       GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
     // [END get_token] 
     Log.i(TAG, "GCM Registration Token: " + token); 

     // TODO: Implement this method to send any registration to your app's servers. 
     sendRegistrationToServer(token); 

     // Subscribe to topic channels 
     subscribeTopics(token); 

     // You should store a boolean that indicates whether the generated token has been 
     // sent to your server. If the boolean is false, send the token to your server, 
     // otherwise your server should have already received the token. 
     sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply(); 
     // [END register_for_gcm] 
    } catch (Exception e) { 
     Log.d(TAG, "Failed to complete token refresh", e); 
     // If an exception happens while fetching the new token or updating our registration data 
     // on a third-party server, this ensures that we'll attempt the update at a later time. 
     sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply(); 
    } 
    // Notify UI that registration has completed, so the progress indicator can be hidden. 
    Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE); 
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
} 

/** 
* Persist registration to third-party servers. 
* 
* Modify this method to associate the user's GCM registration token with any server-side account 
* maintained by your application. 
* 
* @param token The new token. 
*/ 
private void sendRegistrationToServer(String token) { 
    // Add custom implementation, as needed. 
} 

/** 
* Subscribe to any GCM topics of interest, as defined by the TOPICS constant. 
* 
* @param token GCM token 
* @throws IOException if unable to reach the GCM PubSub service 
*/ 
// [START subscribe_topics] 
private void subscribeTopics(String token) throws IOException { 
    GcmPubSub pubSub = GcmPubSub.getInstance(this); 
    for (String topic : TOPICS) { 
     pubSub.subscribe(token, "/topics/" + topic, null); 
    } 
} 
// [END subscribe_topics] 

} 
+0

Utilizzare il servizio per quello o la migliore idea possibile è utilizzare il ricevitore di trasmissione con gestore allarmi.Il vostro tempo per la notifica o qualsiasi cosa volete impostare nella classe –

+0

avete scritto ricevitore di trasmissione? –

+0

è sufficiente il solo ricevitore di trasmissione. –

risposta

0

Secondo la dichiarazione ufficiale di MCG funziona con Google riprodurre la libreria dei servizi in modo da provare a controllare se il tuo cellulare ha gli ultimi aggiornamenti di Google Play Services installati anche a volte internet problema di connettività di rete conject le cause il problema con la notifica push GCM oppure è possibile controllare la ricevuta di consegna se il destinatario ha effettivamente ricevuto il messaggio o meno. Ma quello che dici che GCM non funziona quando app non è in primo piano non è corretto

+1

Ho i servizi di gioco di google più recenti e aggiornati. Inoltre non ci sono problemi nella connessione Internet, posso ricevere le notifiche di Facebook. Quando l'app è aperta e ridotta a icona o in background, funzionerebbe, ma se la si chiude, non funzionerà già – heyou

0

Quando l'applicazione viene chiusa con forza da parte dell'utente: le notifiche non arrivano

E 'una caratteristica della piattaforma Android . L'interruzione forzata di un'applicazione da parte dell'utente mette l'applicazione in uno stato di arresto e non viene eseguito alcun codice, compresi gli eventuali broadcast receivers dichiarati in manifest. Solo quando l'utente avvia esplicitamente l'app viene messo in uno stato in cui i ricevitori vengono licenziati.

Per ulteriori documentazione relativa Force Stop, segui questo link:

+0

non è corretto, perché la notifica di whatsap arriva bene –

0

Una semplice soluzione per questo problema è che, "la massima priorità insieme, mentre la costruzione vostra notifica" . La costante di priorità varia nell'intervallo da -2 a 2 (dal più basso al più alto) e 0 è il valore predefinito per questo campo. Spero che questo ti sia d'aiuto. Grazie.