2016-08-04 141 views
0

我正在使用GCM實現聊天應用程序。我有一個擴展GcmListenerService的自定義推送接收器。當我按下推送通知,如果應用程序在後臺,應用程序崩潰。這是處理應用程序在後臺時通知的代碼當通知托盤按下推送通知時,Android GCM崩潰應用程序

// verifying whether the app is in background or foreground 
      if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
       Log.d(TAG, "App is not in background"); 

       // app is in foreground, broadcast the push message 
       Intent pushNotification = new Intent(Config.ACTION_PUSH_NOTIFICATION); 
       pushNotification.putExtra("type", Config.PUSH_TYPE_CHATROOM); 
       pushNotification.putExtra("message", message); 
       pushNotification.putExtra("chat_room_id", chatRoomId); 
       sendBroadcast(pushNotification); 

       // play notification sound 
       NotificationUtils notificationUtils = new NotificationUtils(); 
       notificationUtils.playNotificationSound(); 
      } else { 
       Log.d(TAG, "App is in background"); 
       // app is in background. show the message in notification try 
       Intent resultIntent = new Intent(getApplicationContext(), ChatRoomActivity.class); 
       resultIntent.putExtra("chat_room_id", chatRoomId); 
       showNotificationMessage(getApplicationContext(), title, user.getName() + " : " + message.getMessage(), message.getCreatedAt(), resultIntent); 
      } 

而且NotificationUtils是一個帶有句柄通知的自定義類。這裏是showNotificationMessage方法:

public NotificationUtils(Context mContext) { 
    this.mContext = mContext; 
} 
public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) { 
// notification icon 
    final int icon = R.mipmap.ic_launcher1; 

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    final PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        mContext, 
        0, 
        intent, 
        PendingIntent.FLAG_CANCEL_CURRENT 
      ); 

    final NotificationCompat.Builder mBuilder = new  NotificationCompat.Builder(
      mContext); 
Notification notification; 
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0) 
      .setAutoCancel(true) 
      .setContentTitle(title) 
      .setContentIntent(resultPendingIntent) 
      .setSound(alarmSound) 
      .setStyle(inboxStyle) 
      .setWhen(getTimeMilliSec(timeStamp)) 
      .setSmallIcon(R.drawable.ic_notification_small) 
      .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon)) 
      .setContentText(message) 
      .build(); 

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(Config.NOTIFICATION_ID, notification); 

我跟着其他問題,指定應該添加到清單文件的內容。我把他們都加了進來。這裏也是權限的一個片段:

<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> <permission 
    android:name="de.hassib.ble_heart_rate_test.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
<uses-permission android:name="de.hassib.ble_heart_rate_test.permission.C2D_MESSAGE" /> 
<uses-permission android:name="de.hassib.ble_heart_rate_test.permission.C2D_MESSAGE" /> 

接收器應用:

 <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
      <category android:name="de.hassib.ble_heart_rate_test" /> 
     </intent-filter> 
    </receiver> 

服務:

 <service 
     android:name=".service.MyGcmPushReceiver" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".service.GcmIntentService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID" /> 
     </intent-filter> 
    </service> 

任何想法,我做錯了什麼?

回答

1

我不明白你爲什麼要進行驗證,因爲必須在具有PendingIntent的Notification構建器中設置操作,而且不管我是否背景。

我的意思是這樣的:

PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setContentIntent(resultPendingIntent); 

但你可以驗證getApplicationContext()因爲當應用程序已被殺害不可用。

希望得到這個幫助。

+0

我確實在方法調用的notificationUtils類中使用了pendingIntent。我編輯了問題並添加了通知處理代碼片段。 –

0

這是由您設置爲通知的圖標引起的,因爲它太大。你應該調整你的圖標是這樣的:

  • LDPI:48×48
  • MDPI:64×64
  • 華電國際:96×96
  • xhdpi:128×128
  • xxhdpi:192x192
  • xxxhdpi:256×256

如果我使用上面提到的圖標大小,它的效果很好。