2016-12-09 50 views
-7

在獲取任何推送通知時(而不是在每個設備中)有時會收到此錯誤 - 可能是設備問題? 最新在Android 5.0中崩潰了在GcmListenerService中獲取錯誤

請幫忙!!

致命異常:顯示java.lang.NullPointerException:嘗試在android.os.Parcel上一個空 對象引用 調用 虛擬方法java.lang.String中java.lang.String.intern()' .readException(Parcel.java:1556) 在android.os.Parcel.readException(Parcel.java:1499) 在android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:3740) 在android.app.PendingIntent.getActivity (PendingIntent.java:291) at android.app.PendingIntent.getActivity(PendingIntent.java:252) at com.mystuc.Service.MyGcmListenerService.sendNotification(MyGcmListenerServi ce.java:122) at com.mystuc.Service.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:82) at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source) at com.google.android .gms.gcm.GcmListenerService.zzp(未知來源) at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source) at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source) at com.google.android.gms.gcm.GcmListenerService $ 1.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker。運行(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

我使用這個代碼:

public class MyGcmListenerService extends GcmListenerService { 
private static final String TAG = "MyGcmListenerService"; 
private Intent intent; 
@Override 
public void onMessageReceived(String from, Bundle data) { 
    Log.e("onMessageReceived", "onMessageReceived:" + data.toString()); 
    Utils pref = Utils.getInstance(getApplicationContext()); 
    String uid = pref.getString(Constants.uid, ""); 
    if (uid.length() > 0) { 
     if (pref.getBoolean(Constants.isEnableNotification, false)) { 

      try { 
       String response = data.getString("data"); 
       if (response != null) { 
        JSONObject jsonObject = new JSONObject(response); 
        if (jsonObject.has("stype")) { 
         String stype = jsonObject.getString("stype"); 
         if (stype.equalsIgnoreCase("timeline")) { 
          String msg = jsonObject.getString("msg"); 
          String post_type = jsonObject.getString("post_type"); 
          String post_id; 
          String postDetails = ""; 
          if (post_type.equalsIgnoreCase(Constants.PostType.Question)) { 
           post_id = jsonObject.getString("question_id"); 
          } else { 
           post_id = jsonObject.getString("post_id"); 
           try { 
            postDetails = jsonObject.getJSONArray("postDetail").getJSONObject(0).toString(); 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 
          } 
          String post_tab = jsonObject.getString("post_tab"); 


          String message = msg.trim(); 
          if (post_type.equalsIgnoreCase(Constants.PostType.Question)) { 

           intent = new Intent(getApplicationContext(), QuestionView.class); 
          } else { 
           intent = new Intent(getApplicationContext(), FeedDetail_home.class); 
          } 


          intent.putExtra(Constants.fromTimeLineNotification, true); 
          intent.putExtra(Constants.posttype, post_type); 
          intent.putExtra(Constants.postid, post_id); 
          intent.putExtra(Constants.posttab, post_tab); 
          intent.putExtra(Constants.postDetailsJson, postDetails); 
          intent.setAction(Intent.ACTION_MAIN); 
          intent.addCategory(Intent.CATEGORY_LAUNCHER); 
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
          sendNotification(message, intent); 
         } 
        } else { 
         String msg = jsonObject.getString("msg"); 

         String sender = jsonObject.getString("sent_by"); 
         String templeteid = jsonObject.getString("template_id"); 
         String message = msg.trim(); 
         Intent intent = new Intent(this, Home.class); 
         intent.putExtra(Constants.fromNotification, true); 
         intent.setAction(Intent.ACTION_MAIN); 
         intent.addCategory(Intent.CATEGORY_LAUNCHER); 
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
         sendNotification(message, intent); 
        } 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    sendBroadcast(); 
} 


public void sendBroadcast() { 
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.OnNotification)); 
} 

private void sendNotification(String message, Intent intent) { 
    Bitmap icon = BitmapFactory.decodeResource(
      MyGcmListenerService.this.getResources(), 
      R.drawable.app_icon); 
    final Intent notificationIntent = new Intent(this, Home.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    notificationIntent.setAction(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); 
    int color = getResources().getColor(R.color.colorPrimary); 
    Uri sound = Uri.parse("android.resource://com.mystuc/raw/coindrop"); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_smallnotify) 
      .setContentTitle("MyStuC") 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setColor(color) 
      .setSound(sound) 
      .setLargeIcon(icon) 
      .setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_MAX); 

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

    notificationManager.notify(0 , notificationBuilder.build()); 
}} 
+1

請說明您的問題@Gurvinder –

+0

我已經加入一些更多的細節,請檢查。謝謝 –

回答

0

而不是給予0,

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

把那uniqueint