Reputation: 171
I am using GCM to implement a chat application. I have a custom push receiver extending GcmListenerService. When I press on the push notification if the application is in the background, the app crashes. Here is the code that handles the notification when the app is in the background
// 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);
}
And the NotificationUtils is a custom class with handles notifications. Here is the showNotificationMessage method:
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);
I followed other questions which specified what should be added to the manifest file. I added them all. Here is also a snippet of the permissions:
<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" />
Receiver in application:
<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:
<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>
Any ideas what I am doing wrong here?
Upvotes: 0
Views: 700
Reputation: 426
I don't understand why you are making that validation because the actions has to be set in the Notification builder with a PendingIntent and no matter if is i background or not.
I mean something like this:
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
But you can verify the getApplicationContext()
because it is not available when the app has been killed.
Upvotes: 1
Reputation: 71
It's caused by the icon you set to your notification, since it is too large. You should resize your icon like this:
If I use the icon sizes mentioned above, it works great.
Upvotes: 0