2013-03-13 12 views
5

Sto cercando di inviare le notifiche push per un telefono con GCM, funziona benissimo su un dispositivo con Android 4, ma quando provo a trasmettere la notifica su Android 2.3 l'applicazione si è schiantato e sto ottenendo questo erroreLe notifiche push su Android 2.3 si sono bloccate.

03-13 11:44:25.994: E/AndroidRuntime(3579): FATAL EXCEPTION: IntentService[GCMIntentService-1074787013996-1] 
03-13 11:44:25.994: E/AndroidRuntime(3579): java.lang.IllegalArgumentException: contentIntent required: pkg=com.itom.vreauRCA id=0 notification=Notification(vibrate=default,sound=default,defaults=0xffffffff) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.os.Parcel.readException(Parcel.java:1251) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.os.Parcel.readException(Parcel.java:1235) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.app.NotificationManager.notify(NotificationManager.java:110) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.app.NotificationManager.notify(NotificationManager.java:90) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at com.itom.vreauRCA.GCMIntentService.generateNotification(GCMIntentService.java:71) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at com.itom.vreauRCA.GCMIntentService.onMessage(GCMIntentService.java:36) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.os.Looper.loop(Looper.java:123) 
03-13 11:44:25.994: E/AndroidRuntime(3579):  at android.os.HandlerThread.run(HandlerThread.java:60) 
03-13 11:44:26.374: V/GCMBroadcastReceiver(3579): onReceive: com.google.android.c2dm.intent.RECEIVE 
03-13 11:44:26.374: V/GCMBroadcastReceiver(3579): GCM IntentService class: com.itom.vreauRCA.GCMIntentService 
03-13 11:44:26.374: V/GCMBaseIntentService(3579): Acquiring wakelock 
03-13 11:44:26.414: V/GCMBroadcastReceiver(3579): onReceive: com.google.android.c2dm.intent.RECEIVE 
03-13 11:44:26.414: V/GCMBroadcastReceiver(3579): GCM IntentService class: com.itom.vreauRCA.GCMIntentService 
03-13 11:44:26.414: V/GCMBaseIntentService(3579): Acquiring wakelock 

questo è il mio classe GCMBaseIntentService

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

import com.google.android.gcm.GCMBaseIntentService; 

public class GCMIntentService extends GCMBaseIntentService { 
    private static final String TAG = "GCMIntentService"; 
    PendingIntent contentIntent; 

    public GCMIntentService() { 
     super(GetObiecte.SENDER_ID); 
    } 

    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
     Log.d("GCMIntentService", "in GCMIntentService"); 
    } 

    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     Log.i(TAG, "Device unregistered"); 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "Received message"); 
     String a = intent.getStringExtra("data"); 
     String b = intent.getStringExtra("data2"); 
     generateNotification(context, a,b); 
    } 

    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     Log.i(TAG, "Received deleted messages notification"); 
    } 

    @Override 
    public void onError(Context context, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     Log.i(TAG, "Received recoverable error: " + errorId); 
     return super.onRecoverableError(context, errorId); 
    } 

    private void generateNotification(Context context, String message, 
               String title) { 

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

     Notification notification = new Notification(); 
     notification.icon = R.drawable.ic_launcher; 
     notification.tickerText = "i-Asigutare"; 
     notification.defaults = Notification.DEFAULT_ALL; 
     notification.setLatestEventInfo(context, title, message, null); 
     notificationManager.notify(0, notification); 
    } 
} 

risposta

3

Il problema è sul metodo notification.setLatestEventInfo.

Questo metodo era deprecated in livello API 11 e l'utilizzo di Notification.Builder è consigliato. È inoltre possibile consultare NotificationCompat.Builder per l'utilizzo con la libreria di supporto.

Se si vuole ancora andare con il vostro modo, si dovrebbe guardare la risposta accettata su questa questione: Android - notification manager, having a notification without an intent

che chiaramente membri che usano come questo

notification.setLatestEventInfo(context, contentTitle, contentText, PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)); 
0

notification.setLatestEventInfo(context, title, message, null); invece di null avete per aggiungere un intent, che verrà avviato, quando l'utente tocca la notifica.

Dal android developer page:

contentIntent: L'intento di lanciare quando l'utente fa clic la notifica espanso. Se si tratta di un'attività, è necessario includere il flag FLAG_ACTIVITY_NEW_TASK, che richiede che si prenda cura della gestione delle attività come descritto nel documento Attività e stack posteriore.