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

时间:2016-09-30 09:50:21

标签: android notifications handler

我创建了一个按钮。在我创建构建器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);

}

4 个答案:

答案 0 :(得分:0)

用以下代码替换代码:

Error: Unable to access jarfile app.jar

答案 1 :(得分:0)

使用以下代码在6秒后消失通知,只需将MainActivity.this替换为您的活动名称

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);

答案 2 :(得分:0)

试试这个

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

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

                }
            }, 6000);

答案 3 :(得分:0)

实际上你说的是不可能的。推送通知始终来自服务器端或控制台端。我不知道你要做什么。如果你已完成服务器端编码,只需在线程

中替换此代码

例如。

    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

    }