【问题标题】:How to Return to activity after the notification has appeared? Notification is not clickable通知出现后如何返回活动?通知不可点击
【发布时间】:2020-07-18 14:44:44
【问题描述】:

我有问题 - 单击收到的通知后无法返回主活动。通知本身不可点击。通知在 onPause 方法中触发(在后台)

我的主要活动是 MainActivity,它是通过 PendingIntent 和 AlarmManager 设置通知

public class MainActivity extends AppCompatActivity {

private NotificationManager notificationManager;

private static final int NOTIFY_ID = 1;

private static final String CHANNEL_ID = "CHANNEL_ID";

NotificationPublisher notificationPublisher;

Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createNotificationChannel();
}

private void createNotificationChannel() {

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
        CharSequence name = "LemubitReminderChannel";
        String description = "Channel for Lemubit Reminder";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("notifyLemubid", name, importance);
        channel.setDescription(description);

        
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

}

private void reminderMethod (){
    Toast.makeText(this, "Напоминание о запуске фонового уведомления", Toast.LENGTH_SHORT).show();

    Intent intent = new Intent(getApplicationContext(), NotificationPublisher.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    long inPauseTime = System.currentTimeMillis();
    long ten = 2 * 1000;
    alarmManager.set(AlarmManager.RTC_WAKEUP, inPauseTime + ten, pendingIntent);
}

@Override
protected void onPause() {
    super.onPause();
    reminderMethod();
}
}

广播接收器

public class NotificationPublisher extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

    int unicode = 0x1F62D;



    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubid")
            .setAutoCancel(true)
            .setContentTitle("текст " + getEmojiByUnicode(unicode))
            .setContentText("текст")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
    notificationManagerCompat.notify(200, builder.build());

}

public String getEmojiByUnicode(int unicode){
    return new String(Character.toChars(unicode));
}
}

【问题讨论】:

  • BroadcastReceiver 是否被触发? Notification 会显示吗?
  • 由于您的Intent 是用于BroadcastReceiver,您应该删除标志FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK。如果您有 Intent 对应 Activity,则使用这些标志。
  • 我删除了它们,但没有帮助。是否可以使用 getBroadcast 恢复活动?
  • 请回答我的问题。 BroadcastReceiver 是否被触发(onReceive() 是否被调用)? Notification 会显示吗?
  • 我认为是的,通知会显示

标签: java broadcastreceiver android-notifications alarmmanager android-pendingintent


【解决方案1】:

你需要在Notification上设置contentIntent,那么当用户点击Notification时,contentIntent就会被调度。如果您希望用户打开您的应用程序的Activity,那么只需为您的Activity 创建一个Intent,将其包装在PendingIntent 中并在“通知”上调用setContentIntent()。有数百个关于如何做到这一点的方法和教程,只需搜索一下即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多