2016-08-04 19 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メソッドがあります:

私はマニフェストファイルに何を追加するべきかを指定した他の質問に続きました。私はそれらをすべて加えました。ここでまた、権限の抜粋です:アプリケーションで

<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で通知ビルダにアクションを設定する必要があるため、その検証を行う理由を理解できませんでした。

私はこのような何か意味:

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
  • hdpi:に96x96
  • xhdpi:128×128
  • xxhdpi:192x192
  • xxxhdpi:256×256

上記のアイコンサイズを使用すると効果的です。

関連する問題