有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在单击通知时将对象传递给notification builder中的另一个活动

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

在我们的应用程序中groupchat有多个事件。如果我单击通知,它会将我带到右侧的聊天窗口,因此我已通过putExtra传递了eventMO包裹。在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) 个答案

  1. # 1 楼答案

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

    int notification_id = 123;
    
    notificationManager.notify(notification_id, notification);
    
  2. # 2 楼答案

    我在我的项目中使用这段代码

    通知

            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)
    {
    ...
    }