通知不开Acivity的onclick [英] Notification Not open Acivity onCLick

查看:115
本文介绍了通知不开Acivity的onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建点击它的时候应该打开活动的通知。但是,当我点击通知活动不开。
任何帮助将是AP preciated。这里是我的code:

  NotificationManager notificationManager =(NotificationManager)上下文
            .getSystemService(Context.NOTIFICATION_SERVICE);    意向意图=新意图(背景下,MessageReceivedActivity.class);
    intent.putExtra(有效载荷,有效载荷);
    intent.setAction(Long.toString(System.currentTimeMillis的()));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    的PendingIntent的PendingIntent = PendingIntent.getActivity(上下文,0,
            意图,PendingIntent.FLAG_CANCEL_CURRENT);    Notification.Builder通知=新Notification.Builder(上下文)
    .setContentTitle(收到消息)
    .setSmallIcon(R.drawable.icon)
    .setContentText(负载)
    .setContentIntent(的PendingIntent)
    .setAutoCancel(真);
    通知notificationn = notification.getNotification();    notificationManager.notify(0,notificationn);


解决方案

使用这样的:

  Notification.Builder通知=新Notification.Builder(上下文)
.setContentIntent(getDialogPendingIntent(文字,intentName));
私人的PendingIntent getDialogPendingIntent(字符串dialogText,
            字符串intentname){
        返回PendingIntent.getActivity(
                的背景下,
                dialogText.hash code()
                新意图(ACTION_DIALOG)
                        .putExtra(Intent.EXTRA_TEXT,dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .setAction(intentname),0);    }


  

getDialogPendingIntent(文字,intentName) intentName = com.yourProject.exrta.yourIntentName


如果你愿意,你可以改变addFlags或putExtra。


如果呼叫意图名称不起作用像该类使用它,它必须工作:

 意图notificationIntent =新意图(MainActivity.this,TestActivity.class);
                    //设置的意图,因此它不会启动新活动
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    的PendingIntent意图=
                            PendingIntent.getActivity(MainActivity.this,0,notificationIntent,0);
NotificationManager notificationManager =(NotificationManager)MainActivity.this
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                Notification.Builder通知=新Notification.Builder(MainActivity.this)
                .setContentTitle(收到消息)
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(真)
                .setContentIntent(意向);                  通知notificationn = notification.getNotification();
                  notificationManager.notify(1,notificationn);

I want to create a notification which should open activity when click on it. But when i click on notification activity not open . Any help would be appreciated .Here is my code:

NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);





    Intent intent = new Intent(context, MessageReceivedActivity.class);
    intent.putExtra("payload", payload);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);



    Notification.Builder notification = new Notification.Builder(context)


    .setContentTitle("Message Received")
    .setSmallIcon(R.drawable.icon)
    .setContentText(payload)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true);






    Notification notificationn = notification.getNotification();

    notificationManager.notify(0, notificationn);

解决方案

Use this :

Notification.Builder notification = new Notification.Builder(context)
.setContentIntent(getDialogPendingIntent(Text, intentName));


private PendingIntent getDialogPendingIntent(String dialogText,
            String intentname) {
        return PendingIntent.getActivity(
                context,
                dialogText.hashCode(),
                new Intent(ACTION_DIALOG)
                        .putExtra(Intent.EXTRA_TEXT, dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .setAction(intentname), 0);

    }

getDialogPendingIntent(Text, intentName) : intentName=com.yourProject.exrta.yourIntentName

you can Change "addFlags" or "putExtra" if you want.


if Call with Intent Name doesn't work use it with class like that and it must work :

    Intent notificationIntent = new Intent(MainActivity.this, TestActivity.class);
                    // set intent so it does not start a new activity
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent intent =
                            PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);


NotificationManager notificationManager = (NotificationManager) MainActivity.this
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                Notification.Builder notification = new Notification.Builder(MainActivity.this)
                .setContentTitle("Message Received")
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(intent);



                  Notification notificationn = notification.getNotification();
                  notificationManager.notify(1, notificationn);

这篇关于通知不开Acivity的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆