android - 通过点击通知显示应用程序

标签 android

我知道有人问过几个类似的问题,但答案都包含已弃用的解决方案。

我已经开始在 android 上开发一个应用程序,该应用程序显示有关通知的一些信息(始终保持打开状态),我希望用户能够通过点击通知来转到该应用程序。

下面是我所做的,但没有用,因为当我点击通知(正确显示)时没有任何反应

我有一个名为 SendNotification 的方法,它由按键调用,除了调用之外几乎没有其他作用

private void DispatchNotification(String numValue,String nameValue){
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
        builder.setAutoCancel(false);
        builder.setContentTitle("Just Title");
        builder.setContentText(nameValue+" "+numValue);
        builder.setSmallIcon(R.drawable.abc_popup_background_mtrl_mult);
        Notification notification = builder.build();
        notification.flags|= Notification.FLAG_ONGOING_EVENT;
        builder.addAction(R.drawable.abc_popup_background_mtrl_mult,"MENU",null);
        builder.setContentIntent(pIntent);
        NotificationManager manager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
        manager.notify(8,notification);
    }

提前致谢。

最佳答案

第 1 步:摆脱现有的 builder.build() 调用。

第 2 步:将 notification.flags|= Notification.FLAG_ONGOING_EVENT; 替换为 builder.setOngoing(true);

第 3 步:将 manager.notify(8,notification) 替换为 manager.notify(8, builder.build());

IOW,在完成 Builder 之前,您正在构建您的 Notification

关于android - 通过点击通知显示应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727785/

相关文章:

android - Android 上的触摸屏

android - 我的复选框没有出现在我的 CheckedTextView

android - 与 auth.extendSSOAccessToken 相关的 Facebook 错误

Android Media Player 挂起或返回 null

android - 应用程序模块也可以用作库模块吗?

android - 在 ListView 之上添加一个 View 并在向下滚动时移动所有内容?

android - 将用户密码存储在 FirebaseDatabase 中是个好主意吗?

android - 在智能手机上离线使用自定义谷歌操作

java - 如何使用 EditText 编辑全局变量以将值传递给其他 Activity ?

android - 如何在条形图中启用滚动并将 xAxis 标签与条形图组对齐?