单击通知时,将对象传递给通知构建器中的另一个活动

时间:2015-12-08 08:02:00

标签: java android

这里有一项名为grouchat的活动。消息由gcmIntent类发送和接收。在该课程中,我有代码向移动设备发出通知。

在我们的应用中,groupchat有多个活动。如果我点击通知,它应该带我到正确的聊天窗口,所以我通过了eventMO putExtra parcelable。在groupchat中,有代码可以获得parcelable

以下是groupchat活动中的代码:

eventMO = (EventMO) getIntent().getParcelableExtra("eventMo");
List<ChatMO> chatMOs = dbHelper.getGroupChatMessageForEvent(eventMO.getEventId());

以下代码位于gcmIntend类中以构建通知:

Intent groupChatActFrag = new Intent(getApplicationContext(), GroupChatActivity.class);
//here i have messageMO so i just set the eventid from messageMO to eventMO according to get the eventID in groupchat Activity
EventMO eventMO = new EventMO();
eventMO.setEventId(messageMO.getEventId());
groupChatActFrag.putExtra("eventMo", eventMO);
groupChatActFrag.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, groupChatActFrag, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_dialog_info)
                        .setContentTitle(messageMO.getEventTitle())
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(messageMO.getfromUserName()))
                        .setContentText(messageMO.getMessage())
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

mBuilder.setContentIntent(contentIntent);

mBuilder.setAutoCancel(true);
Log.e("gcm","eventid"+messageMO.getEventId());

mNotificationManager.notify((int) (long) eventMO.getEventId(), mBuilder.build());

如果我点击通知,则会抛出空指针异常,因为我将eventMO作为null。请告诉我如何将GcmIntent课程中的当前活动ID传递给groupchat活动。

2 个答案:

答案 0 :(得分:1)

我在我的项目中使用此代码

通知

        Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(context)//
                .setContentTitle(title == null ? context.getString(R.string.app_name) : title)//
                .setContentText(message)//
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))//
                .setSmallIcon(R.drawable.ic_launcher)//
                .setContentIntent(pendingIntent)//
                .setTicker(context.getString(R.string.app_name))//
                .build();

        notification.defaults |= Notification.DEFAULT_ALL;

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(id, notification);

onCreate和onNewIntent

中的活动检查意图
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    if (getIntent() != null) {
        checkIntent(getIntent());
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    checkIntent(intent);
}

private void checkIntent(Intent intent)
{
...
}

答案 1 :(得分:0)

您需要将getEvent Id更改为通知ID。

int notification_id = 123;

notificationManager.notify(notification_id, notification);