如何在通知Android开放应用程序? [英] How to open app in notification in android?

查看:131
本文介绍了如何在通知Android开放应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,完成了异步文件中的任务时,它显示了一个通知,这个code

In my app, when completing a task in async file, it shows a notification with this code

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, new Intent(), 0);

    Notification noti = new Notification.Builder(context)
    .setContentTitle("Complete")
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .build();

    notificationManager.notify(0, noti); 

问题是,当我点击该通知,没有任何反应。基本上我希望它这样,如果应用程序已经打开,并且用户单击通知,然后什么都不应该打开。如果应用程序没有打开(这意味着它仍在运行,但最小化),那么我想它打开应用程序,像最大化。

The problem is when I click the notification, nothing happens. Basically I want it so that, if the app is already open and the user clicks the notification, then nothing should open. If the app is not open (which means its still running but minimized) then I want it to open the app, like maximize it.

有谁知道如何做到这一点?

Does anyone know how to do this?

感谢

推荐答案

您需要一个意图,样品code:

You need an Intent, sample code:

NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    Intent notificationIntent = new Intent(context, MainActivity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);

这将打开你的 MainActivity

这篇关于如何在通知Android开放应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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