从 Android 的通知中打开 MainActivity

Posted

技术标签:

【中文标题】从 Android 的通知中打开 MainActivity【英文标题】:Openning MainActivity from Notification in Android 【发布时间】:2020-08-09 04:14:54 【问题描述】:

我想从通知中打开我的应用 Mainactivity,但不知道在哪里声明 builder.setContentIntent() 的意图 这是我使用的代码: ReminderBroadcast.java

public class ReminderBroadcast  extends BroadcastReceiver 
    @Override
    public void onReceive(Context context, Intent intent)
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyme")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Title for Notification")
                .setContentText("This notification is Working")
                .setPriority(NotificationCompat.PRIORITY_HIGH);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(200, builder.build());
    

MainActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createNotificationChannel();
        
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Toast.makeText(MainActivity.this, "Reminder set", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, ReminderBroadcast.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent,0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                long timeAtClick = System.currentTimeMillis();
                long fiveSeconds = 5 * 1000;
                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        timeAtClick + fiveSeconds, pendingIntent);
            
        );
    
    private void createNotificationChannel()
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
            CharSequence name = "ReminderChannel";
            String description = "This is used for channel";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel("notifyme" , name , importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        
    

【问题讨论】:

【参考方案1】:

你必须使用PendingIntent

 Intent activityIntent = new Intent(getApplicationContext(), MainActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
            .setContentTitle(title)
            .setSmallIcon(R.drawable.noti)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentText(message);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(200, builder.build());

【讨论】:

几乎成功了!我必须添加 context.getApplicationContext 才能让它工作。另一件事是单击时通知没有被删除。 您不需要使用context.getApplicationContext() - context 可以构建PendingIntent。如果您希望在单击通知时将其删除,听起来您正在寻找 setAutoCancel(true) @ianhanniballake 谢谢。有效。我想我应该更多地了解上下文

以上是关于从 Android 的通知中打开 MainActivity的主要内容,如果未能解决你的问题,请参考以下文章

如何知道应用程序是不是从 android 通知托盘打开?

如何从使用 PushSharp 发送的通知中检测 android 应用程序是不是已打开?

如何清除从 Android 中点击通知打开的先前活动?

如何从 Appium 打开推送通知并阅读 Android/iOS 设备中的文本?

如何从后台服务的全屏意图通知Android中打开活动

从通知-Android打开活动