当通知托盘按下推送通知时,Android GCM崩溃应用程序

问题描述:

我正在使用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> 

任何想法,我做错了什么?

我不明白你为什么要进行验证,因为必须在具有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。我编辑了问题并添加了通知处理代码片段。 –

这是由您设置为通知的图标引起的,因为它太大。你应该调整你的图标是这样的:

  • LDPI:48×48
  • MDPI:64×64
  • 华电国际:96×96
  • xhdpi:128×128
  • xxhdpi:192x192
  • xxxhdpi:256×256

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