【问题标题】:Make notification drawer resume app when clicked单击时使通知抽屉恢复应用程序
【发布时间】:2017-07-26 07:11:27
【问题描述】:

我不知道这里的正确术语是什么,但是当启动通知时,在“通知抽屉”(您从屏幕顶部拉出并查看来自所有通知的大列表的地方)应用程序),有你的应用程序的通知。

我希望在单击此通知时恢复我的应用程序。但是我在通知生成器中看不到任何相关方法。这还有可能吗?

我的代码:

private void launchNotification(Context context) {
    String notificationText = "Click to resume app!";

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder.setSmallIcon(R.drawable.notification_small_icon);
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
    notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    notificationBuilder.setContentText(notificationText);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notificationBuilder.build());
}

【问题讨论】:

    标签: android push-notification callback notifications onclick


    【解决方案1】:

    试试这个...使用通知意图,您可以将应用打开到定向活动。

      Intent notificationIntent = new Intent(context, YourActivity.class);
      PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    
             notificationBuilder.setContentIntent(intent);
    

    【讨论】:

    • 这似乎与我当前的代码生成器不太吻合
    • 我已经更新了我的代码。您可以添加它并打开任何活动。
    • 似乎也不起作用...然后我再次从 DialogFragment 而不是 Activity 调用它,这可能是为什么?
    • 你显然需要一个活动来包含片段。你可以为你的对话使用透明活动
    • 似乎是堆叠活动而不是回到你离开的地方(点击返回按钮返回原始!)
    【解决方案2】:

    试试这个

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
    
            b.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setTicker("Ticker")
                    .setContentTitle("title")
                    .setContentText("message")
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                    .setContentIntent(contentIntent)
                    .setContentInfo("Info");
    
            Random r = new Random();
            int randomNo = r.nextInt(100000000 + 1);
    
            NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(randomNo, b.build());
    

    【讨论】:

    • 不幸的是,似乎不起作用,除了关闭抽屉外,点击没有任何作用
    • 用getApplicationContext()替换getBaseContext()
    • 效果一样
    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多