簡體   English   中英

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替換為您的活動名稱

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM