Ich verwende GCM, um eine Chat-Anwendung zu implementieren. Ich habe einen benutzerdefinierten Push-Empfänger, der GcmListenerService erweitert. Wenn ich auf die Push-Benachrichtigung drücke, wenn die Anwendung im Hintergrund ist, stürzt die App ab. Hier ist der Code, der die Benachrichtigung verarbeitet, wenn die App im Hintergrund istAndroid GCM stürzt ab, wenn die Push-Benachrichtigung aus der Benachrichtigungsleiste
// 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);
}
Und die NotificationUtils ist eine benutzerdefinierte Klasse mit Griffen Benachrichtigungen. Hier ist die showNotificationMessage Methode:
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);
ich andere Fragen gefolgt, die angegeben, was mit der Manifest-Datei hinzugefügt werden soll. Ich habe sie alle hinzugefügt. Hier ist auch ein Ausschnitt der Berechtigungen:
<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" />
Empfänger in der Anwendung:
<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>
Irgendwelche Ideen, was ich hier falsch mache?
Ich verwende pendingIntent in der notificationUtils-Klasse, die die Methode aufruft. Ich habe die Frage bearbeitet und das Snippet mit der Benachrichtigung hinzugefügt. –