单击时通知不执行任何操作

时间:2019-11-28 10:24:27

标签: android android-intent notifications

因此,我想创建一个通知,当单击该通知时,它将打开我的MainActivity并根据我打算添加的内容进行操作。

现在,该通知可以正常运行,但是一旦我单击它,便什么也没有发生。我进行了一些搜索,并对我的代码进行了一些修改:

我已将android:launchMode="singleTop"添加到清单中的活动中。

我已将代码在onNewIntent方法的MainActivity中执行:

@Override
protected void onNewIntent(Intent intent){
    System.out.println("Entering onNewIntent!!!!!!!");

    if(getIntent().getExtras() != null) {
        url = getIntent().getExtras().getString("url");
        System.out.println("URL in Main Activity : " + url);
        if (url != null && url != "") {
            saveFile(url);
            dezipFile();
        }
    }
}

但是什么都没有改变,我已经放置了一些System.out来查看它是否甚至到达了onNewIntent的开头...但是控制台上没有任何显示。

这是我创建通知的代码:

   private void createNotification () {

    Intent downloadIntent = new Intent(getApplicationContext(), MainActivity.class);
    downloadIntent.putExtra("url", url);
    downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent downloadPendingIntent = PendingIntent.getService(getApplicationContext(), 0, downloadIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Notif")
            .setSmallIcon(R.drawable.icon_notif)
            .setContentTitle(getApplicationContext().getString(R.string.notif_title))
            .setContentText(getApplicationContext().getString(R.string.notif_content))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(downloadPendingIntent)
            .setAutoCancel(true);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("notification_download", "Dowload BD infos", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
        builder.setChannelId("notification_download");
    }

    NotificationManagerCompat notification = NotificationManagerCompat.from(getApplicationContext());
    notification.notify(1, builder.build());
}

如果有人知道我做错了,将不胜感激!

2 个答案:

答案 0 :(得分:0)

添加此内容:

 NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Notif")
        .setSmallIcon(R.drawable.icon_notif)
        .setContentTitle(getApplicationContext().getString(R.string.notif_title))
        .setContentText(getApplicationContext().getString(R.string.notif_content))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(downloadPendingIntent)
        .setAutoCancel(true);


 Intent mIntent = new Intent(context, YourActivity.class);
    mIntent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addNextIntent(mIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    builder.setContentIntent(resultPendingIntent);

答案 1 :(得分:0)

您可以设置用户单击通知时启动的活动,如下所示:

Intent resultIntent = new Intent(this, targetActivityClass);
Intent backIntent = new Intent(this, backActivityClass);
Intent[] intentArray = new Intent[]{backIntent, resultIntent};

PendingIntent resultPendingIntent = PendingIntent.getActivities(this, 0, intentArray,
                PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);    
builder.setContentIntent(resultPendingIntent);

此代码还设置了用户在单击通知后单击“后退”按钮时启动的活动。