我正在尝试将附加内容中的一些数据从gcm服务发送到我的活动,但该捆绑包将变为空。 我尝试过使用Bundle extra和intent extras方法,并尝试在onCreate和onNewIntent方法中接收它们。 由于某种原因,它没有收到。
以下是我的GcmService中的代码: -
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(AppConstants.KEY_RESERVATION_ID, 0);
bundle.putString(AppConstants.KEY_HOST_OR_CONCIERGE_ID, fromId);
bundle.putInt(AppConstants.KEY_HOST_TYPE, MessageThreadDao.TYPE_HOST);
intent.putExtra(AppConstants.KEY_RESERVATION_ID, 0);
intent.putExtra(AppConstants.KEY_HOST_OR_CONCIERGE_ID, fromId);
intent.putExtra(AppConstants.KEY_HOST_TYPE, MessageThreadDao.TYPE_HOST);
intent.putExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String soundPref = Utility.getStringSharedPreference(this, AppConstants.PREFS_SOUND);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setAutoCancel(true)
.setContentTitle(getString(R.string.rental_notification)).setContentText(msg);
if (soundPref.equals("1"))
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
else {
mBuilder.setSound(Uri.parse(""));
}
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// mBuilder.setColor(getResources().getColor(R.color.header_blue));
// }
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
以下是我的活动中的代码: -
//In onCreate
mReservationId = getIntent().getExtras().getInt(AppConstants.KEY_RESERVATION_ID, 0);
mHostOrConciergerId = getIntent().getExtras().getInt(AppConstants.KEY_HOST_OR_CONCIERGE_ID, 0);
mHostType = getIntent().getExtras().getInt(AppConstants.KEY_HOST_TYPE, 0);
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
try {
if (intent.getExtras() != null) {
mReservationId = intent.getExtras().getInt(AppConstants.KEY_RESERVATION_ID, 0);
mHostOrConciergerId = intent.getExtras().getInt(AppConstants.KEY_HOST_OR_CONCIERGE_ID, 0);
mHostType = intent.getExtras().getInt(AppConstants.KEY_HOST_TYPE, 0);
}
retrieveChatHistoryFromDB();
} catch (Exception ignore) {
}
}
Android Manifest中的: -
<activity
android:name=".activity.ChatActivity"
android:label="@string/messages"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />
我也没有设置任何标志。只是一个简单的意图,仍然没有收到数据。
答案 0 :(得分:0)
您可以从GCM服务类
中删除此代码 Bundle bundle = new Bundle();
bundle.putInt(AppConstants.KEY_RESERVATION_ID, 0);
bundle.putString(AppConstants.KEY_HOST_OR_CONCIERGE_ID, fromId);
bundle.putInt(AppConstants.KEY_HOST_TYPE, MessageThreadDao.TYPE_HOST);
intent.putExtras(bundle);
答案 1 :(得分:0)
尝试更改活动manifest.xml的启动模式
android:launchMode="singleTop"
答案 2 :(得分:0)
从代码中删除以下行
intent.putExtra(AppConstants.KEY_RESERVATION_ID, 0);
intent.putExtra(AppConstants.KEY_HOST_OR_CONCIERGE_ID, fromId);
intent.putExtra(AppConstants.KEY_HOST_TYPE, MessageThreadDao.TYPE_HOST);
应该是这样的
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(AppConstants.KEY_RESERVATION_ID, 0);
bundle.putString(AppConstants.KEY_HOST_OR_CONCIERGE_ID, fromId);
bundle.putInt(AppConstants.KEY_HOST_TYPE, MessageThreadDao.TYPE_HOST);
intent.putExtras(bundle);
您想要在NewNetent()上接收的活动应该有
android:launchMode="singleTop"
或添加标志t0 intent
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);