【问题标题】:How to open app in notification in android?如何在android的通知中打开应用程序?
【发布时间】:2014-12-18 11:06:38
【问题描述】:

在我的应用程序中,当完成异步文件中的任务时,它会显示带有此代码的通知

    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); 

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

有人知道怎么做吗?

谢谢

【问题讨论】:

    标签: android notifications push-notification


    【解决方案1】:

    你需要一个Intent,示例代码:

    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

    【讨论】:

    • 这行得通,但是有没有一种方法可以在点击通知时检查应用程序是否正在运行或退出。如果它退出了,那么我想打开另一个类,否则就最大化它。
    • 也许你可以在这里查看stackoverflow.com/questions/15239889/…
    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多