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

时间:2016-08-04 21:29:45

标签: android push-notification google-cloud-messaging

我正在使用GCM来实现聊天应用程序。我有一个自定义推送接收器扩展GcmListenerService。当我按下推送通知时如果应用程序在后台,应用程序崩溃。以下是在应用程序处于后台时处理通知的代码

 // 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>

我在这里做错了什么想法?

2 个答案:

答案 0 :(得分:1)

我不明白为什么要进行验证,因为必须在具有PendingIntent的Notification构建器中设置操作,无论是否为背景。

我的意思是这样的:

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

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

但您可以验证getApplicationContext(),因为它在应用被杀死时无效。

希望得到这个帮助。

答案 1 :(得分:0)

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

  • ldpi:48x48
  • mdpi:64x64
  • hdpi:96x96
  • xhdpi:128x128
  • xxhdpi:192x192
  • xxxhdpi:256x256

如果我使用上面提到的图标尺寸,它会很有效。