点击通知后,活动不会打开

时间:2017-07-09 13:02:33

标签: android android-notifications

我是Android编程的新手,请帮助我解决我错过的问题。

我收到通知,但我的messageActivity未打开

NotificationManager notificationManager = (NotificationManager)
                                    getSystemService(NOTIFICATION_SERVICE);
                            NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(getApplicationContext())
                                    .setContentTitle("Message from " + name)
                                    .setContentText(message)
                                    .setAutoCancel(true)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setSound(soundUri);
                            if (strVibratePreference)
                                mbuilder.setVibrate(new long[]{100, 100});
                            notificationManager.notify(110, mbuilder.build());
                            Intent intent=new Intent(MessageService.this,MessageActivity.class);
                            intent.putExtra("name",name);
                            intent.putExtra("phno",_from);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            PendingIntent pendingIntent = PendingIntent.getActivity(MessageService.this,0,intent,0);
                            mbuilder.setContentIntent(pendingIntent);

2 个答案:

答案 0 :(得分:0)

添加此结束代码

Notification n = mbuilder.build();
notificationManager.notify(YOUR_NOTIF_ID, n);

答案 1 :(得分:0)

  Intent intent = new Intent(MessageService.this, MessageActivity.class);
                            intent.putExtra("name",name);
                            intent.putExtra("phno",_from);
                            PendingIntent pIntent = PendingIntent.getActivity(MessageService.this, (int) System.currentTimeMillis(), intent, 0);

                            // Build notification
                            // Actions are just fake
                            Notification noti = new Notification.Builder(MessageService.this)
                                    .setContentTitle("New mail from " + "test@gmail.com")
                                    .setContentText("Subject")
                                    .setContentIntent(pIntent)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .build();

                            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                            // hide the notification after its selected
                            noti.flags |= Notification.FLAG_AUTO_CANCEL;

                            notificationManager.notify(0, noti);