android:点击按钮几秒后显示推送通知?

android: Show push notification after few seconds on button click?

我创建了一个按钮。在我创建生成器 Notification 之后。 我不知道如何在这个通知中应用 runnable 接口 这个想法是当我点击按钮时,10 秒后应该显示通知。

public void clickbutton (View view){
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // show toast here...
        }
    }, 6000); // 6 seconds
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.notification_icon);
    mBuilder.setContentTitle("My notification");
    mBuilder.setContentText("Hello World!");
    Notification notifcation = mBuilder.build();

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    nm.notify(2, notifcation);

}

用这些替换代码:

public void clickbutton (View view){
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // show toast here...
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.notification_icon);
    mBuilder.setContentTitle("My notification");
    mBuilder.setContentText("Hello World!");
    Notification notifcation = mBuilder.build();

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    nm.notify(2, notifcation);

        }
    }, 6000); // 6 seconds

}

使用以下代码在 6 秒后显示通知,只需将 MainActivity.this 替换为您的 activity 名称

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this);
                mBuilder.setSmallIcon(R.mipmap.ic_launcher);
                mBuilder.setContentTitle("My notification");
                mBuilder.setContentText("Hello World!");
                Notification notifcation = mBuilder.build();

                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                nm.notify(2, notifcation);
            }
        }, 6000);

试试这个

 new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                   //Add your code to display notification here

                }
            }, 6000);

其实你说的是不可能的。推送通知总是来自服务器端或控制台端。我不知道你想做什么。如果您完成了服务器端编码,只需在线程内替换此代码

例如

    public void clickbutton (View view){
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // show toast here...
     int color = getResources().getColor(R.color.pushnotification);

        Intent notificationIntent = new Intent(this, Your_Activity.class);


        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        android.support.v4.app.NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
//                .setContentText("4")
                .setColor(color)
                .setSmallIcon(R.drawable.notification_white)
                .setPriority(Notification.PRIORITY_HIGH)
                .setLights(Color.RED, 300, 300)
                .setContentTitle("Guesp")
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_logo))
                .setStyle((new android.support.v4.app.NotificationCompat.BigTextStyle().bigText("Your message here")));

        mBuilder.setContentIntent(contentIntent);

        int defaults = 0;
        defaults = defaults | Notification.DEFAULT_LIGHTS;
        defaults = defaults | Notification.DEFAULT_VIBRATE;
        defaults = defaults | Notification.DEFAULT_SOUND;

        mBuilder.setDefaults(defaults);
        mBuilder.setContentText(firstname + " " + lastname + " cancelled your booking for " + sportaddress1 + ".");
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

            }
        }, 6000); // 6 seconds

    }