انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل در نوتیفیکشن

مشکل در نوتیفیکشن

#3
سلام
این کد را با کلید 2611 ایجاد میکنید(برای ساخت اطلاعیه ی جدید ) :
کد پی‌اچ‌پی:
NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
    .
setSmallIcon(R.drawable.ic_launcher// notification icon
    
.setContentTitle("Notification!"// title for notification
    
.setContentText("kelidestan.com"// message for notification
    
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(thisMainActivity.class);
PendingIntent pi PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
                (
NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0mBuilder.build()); 
و برای  نمایش و عدم نمایش اطلاعیه با استفاده از سویچ از کد زیر استفاده میکنیم که امکان ذخیره رو هم داخل گذاشتم
کد پی‌اچ‌پی:
Switch swch = (Switch)findViewById(R.id.switch);
       
SharedPreferences shared getSharedPreferences("Prefs"MODE_PRIVATE);
        final 
SharedPreferences.Editor editor shared.edit();
        
boolean boolean_from_sp shared.getBoolean("myBooleanName"true);
        if  (
boolean_from_sp){
            
mNotificationManager.notify(0mBuilder.build());
        }else{
            
mNotificationManager.cancel(0);
        }
        
swch.setChecked(boolean_from_sp);
        
swch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @
Override
            
public void onCheckedChanged(CompoundButton buttonViewboolean isChecked) {
                if (
isChecked)
                    
mNotificationManager.notify(0mBuilder.build());
                 else
                    
mNotificationManager.cancel(0);

                
editor.putBoolean("myBooleanName"isChecked);
                
editor.apply();
            }
        }); 
موفق باشید
پاسخ
 سپاس شده توسط شماره مجازی امارات


پیام‌های این موضوع
مشکل در نوتیفیکشن - توسط saeed.f436 - ۱۳۹۴/۰۵/۱۴, ۱۰:۵۷ ب.ظ
RE: مشکل در نوتیفیکشن - توسط saeed.f436 - ۱۳۹۴/۰۵/۱۴, ۱۱:۰۰ ب.ظ
RE: مشکل در نوتیفیکشن - توسط aliasghar - ۱۳۹۴/۰۵/۱۵, ۰۲:۲۲ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان