2016-09-30 4 views
0

Я создал кнопку. После того, как я создал застройщика Notification. Я не знаю, как применять интерфейс runnable в этом уведомлении Идея заключается в том, когда я нажимаю кнопку, через 10 секунд ей должно быть показано уведомление.android: Показывать уведомление о нажатии через несколько секунд при нажатии кнопки?

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

} 
+0

Ваше требование не ясно :( –

ответ

0

Используйте следующий код для disaply уведомления после 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); 
+0

Thx, у меня была ошибка в NotificationCompat.Builder (this); Не работает «это»? – DIma

+0

на заголовке я упоминал только что заменил MainActivity.this с вашим именем деятельности –

+0

Я знаю, что это ключевое слово не будет работать там, поэтому я поставил MainActivity.this для refrence –

0

Заменить код с этим:

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 

} 
+0

THX, но это NotificationCompat.Builder ошибка (это), почему не работает «это» – DIma

+0

Заменить «это» с классом активности name.this –

+0

?. yup. замените это на YOUR_ACTIVITY.class :) –

0

Try это

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

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

       } 
      }, 6000); 
0

На самом деле вы говорите, что это невозможно. Push-уведомления всегда поступают со стороны сервера или консоли. Я не знаю, что вы пытаетесь сделать. Если вы сделали кодировку на стороне сервера, просто замените этот код внутри резьбы

, например.

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 

    } 
+0

Thx, но у меня была ошибка в NotificationCompat.Builder (это); Не работает «это»? – DIma

+0

Я отредактировал свой ответ. –

Смежные вопросы