我有一个收听云消息的广播接收器,我可以很好地显示通知,但是当用户点击通知时我想用适当的附加功能将它们路由到正确的活动.额外的东西总是空的.
我可以验证传递给挂起的intent的意图在调试时是否有额外的功能.
我尝试了解活动onCreate()方法中的意图
***编辑添加工作代码,有几点需要注意,如果任务已经启动,你不能指望调用oncreate()方法,因此似乎最好的地方是在onresume方法中获取额外的东西.我需要在intent和待定意图上设置标志
@EReceiverpublic class MybroadcastReceiver extends broadcastReceiver {private static final String TAG = "Googlecloudmessage";private static final int NOTIFICATION_ID = 1;private notificationmanager mnotificationmanager;private NotificationCompat.Builder builder;private Context ctx;@Prefpublic MyPrefs_ myPrefs;@OverrIDepublic voID onReceive(Context context, Intent intent) { Log.d(TAG, "received gcm message"); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); this.ctx = context; String messageType = gcm.getMessageType(intent); if (messageType != null) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { } else if (messageType.equalsIgnoreCase("gcm")) { myPrefs.notification().put(true); handleNotification(intent); } } setResultCode(Activity.RESulT_OK);}private voID handleNotification(Intent intent) { Bundle bundle = intent.getExtras(); String typestring = bundle.getString("type"); String icon = bundle.getString("icon"); String Title = null; String body = null; Class<?> intentClass = null; Integer intentExtraID = null; if (typestring == null) return; int type = Integer.parseInt(typestring); switch (type) { case 1: intentClass = FrIEndsActivity_.class; Title = bundle.getString("username"); break; case 2: intentClass = UserProfileActivity_.class; Title = bundle.getString("username"); intentExtraID = Integer.parseInt(bundle.getString("user_ID")); break; } mnotificationmanager = (notificationmanager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);Intent i = new Intent(); i.setClass(ctx, intentClass); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_top | Intent.FLAG_ACTIVITY_CLEAR_top); i.putExtra("gcm", true); if (intentExtraID != null) { i.putExtra("ID", intentExtraID); } int requestID = (int) System.currentTimeMillis(); PendingIntent pendingIntent = PendingIntent.getActivity(ctx, requestID, i, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx); mBuilder.setSmallicon(R.drawable.ic_stat_u); Bitmap bmp = null; try { bmp = Ion.with(ctx, ctx.getResources().getString(R.string.image_container) + icon).asBitmap().get(); } catch (InterruptedException e) { e.printstacktrace(); } catch (ExecutionException e) { e.printstacktrace(); } if (icon != null) { mBuilder.setLargeIcon(bmp); } mBuilder.setContentTitle(Title).setStyle(new NotificationCompat.BigTextStyle().bigText(body)) .setContentText(body); mBuilder.setContentIntent(contentIntent); mnotificationmanager.notify(NOTIFICATION_ID, mBuilder.build());}
}
活动
@OverrIDeprotected voID onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);}@OverrIDeprotected voID onResume() { super.onResume(); Integer newID = getIntent().getExtras().getInt("ID"); ID = newID;}
解决方法:
这是您用于获取PendingIntent的调用:
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, 0);
如果系统中已存在匹配的PendingIntent,则此调用将返回该调用.它可能包含也可能不包含您想要的额外内容.为确保使用您的附加内容,您必须确保更新任何当前的PendingIntent,如下所示:
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
总结 以上是内存溢出为你收集整理的android – 将谷歌云消息接收器中的意图附加内容传递给活动为空全部内容,希望文章能够帮你解决android – 将谷歌云消息接收器中的意图附加内容传递给活动为空所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)