2016-09-28 2 views
-1
import android.app.NotificationManager; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.content.LocalBroadcastManager; 
import android.util.Log; 

import com.google.android.gms.gcm.GcmListenerService; 
import com.htl.jsfshoppingfestival.DataBaseHandler; 

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 
    private NotificationManager mNotificationManager; 
    Context ctx; 
    int countNotification=0; 

    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("Message"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Message: " + message); 

     // Toast.makeText(getApplicationContext(), "Notification :"+message, Toast.LENGTH_LONG).show(); 

     // Toast.makeText(getApplicationContext(),from+message,Toast.LENGTH_LONG).show();  

     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) { 

     DataBaseHandler db = new DataBaseHandler(this); 
     db.addMessage(message); 
     countNotification=db.getRowCount(); 
     db.close(); 

     Log.d("sender", "Broadcasting message"); 
     Intent localIntent = new Intent("badge count"); 
     // You can also include some extra data. 
     localIntent.putExtra("Count", countNotification); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); 
} 
}  

Senden habe ich nicht die Nachricht als eine Benachrichtigung erhalten, wenn ich die Meldung, meine App senden zeigt:Leider App gestoppt Fehler in Benachrichtigung

Leider app In

gestoppt hat Debugging habe ich message=null und App gibt Fehler.

Antwort

0

In welchem ​​Block haben Sie null? OnMessageReceived oder SendNotification?

+0

OnMessageReceived –

+0

dann sicher nichts in 'Bundle' bekommen, überprüfen Sie bitte onec dort –

+0

http://stackoverflow.com/questions/15881033/get-string-from-bundle-android-returns-null über diesen Link –

0

Sie müssen überprüfen, die Nachricht ungleich Null.

if(message != null && !message.trim().isEmpty()) 
{ 
    sendNotification(message); 
} 
Verwandte Themen