通知消息中的待定意图

问题描述 投票:0回答:1

我具有创建通知的功能sendNotification

public class MainActivity extends AppCompatActivity {
    public Handler timerHandler = new Handler();
    public Runnable timerRunnable = new Runnable() {

        @Override
        public void run()
        {
            //timerHandler.postDelayed(this, RECONNECT_TIMEOUT);
            Timber.tag(Utils.TIMBER_TAG).v("Timer is working " );
            //show("Connected to server "+mqttHelper.mqttAndroidClient.isConnected());
            sendNotification("Hi", "Hello");

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Timber.plant(new Timber.DebugTree());
        Timber.tag(Utils.TIMBER_TAG).v("onCreate" );
        setContentView(R.layout.activity_main);
        timerHandler.postDelayed(timerRunnable, 2000);
    }


    @Override
    protected void onDestroy() {

        super.onDestroy();

        Timber.tag(Utils.TIMBER_TAG).v("onDestroy");

    }


    public void sendNotification(String title, String message) {


        int NOTIFICATION_ID = 234;
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String CHANNEL_ID = "my_channel_01";

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            CharSequence name = "my_channel";
            String Description = "This is my channel";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mChannel.setDescription(Description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mChannel.setShowBadge(false);
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
                .setContentTitle(title)
                .setTicker("ticker")
                .setContentText(message)
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
                0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);


        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

}

如果我设置为PendingIntent,则在单击消息时我的应用程序将重新启动:

Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
                0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

这是不可接受的。我需要显示当前正在运行的应用程序,并且不要重新启动它。

如果显示注释,则显示PendingIntent代码消息,但是单击它不会执行任何操作。

如何在单击通知消息时显示MainActivity窗口?

android
1个回答
0
投票

尝试一下:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity, 0, notificationIntent, 0);

设置FLAG_ACTIVITY_NEW_TASK应该只将应用程序的现有任务从后台带到前台,而无需实际创建活动实例。


推荐问答