ورود

View Full Version : نوتیفیکیشن



1yaram
شنبه 04 مرداد 1393, 19:53 عصر
این کد من برای نمایش نوتیفیکیشنه که با کلیک کردن روی یه باتن نوتیفیکیشن مورد نظر به نمایش در میاد
خب حالا من کدی که لازم دارم اینه :
اولا نوتیفیکیشن بایدار باشه و حتی اگه گوشی خاموش بشه اون بالا بمونه دقیقا مثل برنامه : (باتری دکتر)
دوما بتونم یه دکمه دیگه قرار بدم برای حذف کردن نوتیفیکیشن(اگه بشه که آیکون چک باکس بزارم با گذاشتن تیک نوتیفیکیشن بیاد و با برداشتنش بره دیگه عالی میشه):متفکر:


private static final int NOTIFICATION_ID = 1;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("درمورد برنامه");
setContentView(R.layout.about);

int icon = R.drawable.ic_launcher;
//آدرس آیکون موردنظر جهت نمایش در استاتوس بار
CharSequence tickerText = "This is a sample notification";
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "Sample notification";
CharSequence contentText = "This notification has been generated as a result of button click.";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);


/** مقداردهی اولیه Notificationبا استفاده از تنظیمات بالا */
final Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


/** بازیابی مرجع از NotificationManager*/
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);


/** Listener برای کلیک */
Button statusbarnotify = (Button) findViewById(R.id.notific);
statusbarnotify.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
});

1yaram
سه شنبه 07 مرداد 1393, 23:25 عصر
40 نفر نگا کرده کسی جواب نداده
ینی:تشویق:

1yaram
پنج شنبه 09 مرداد 1393, 16:34 عصر
از همه عزیزان ممنون که پاسخ ندادن و در نهایت خودم پیدا کردم:افسرده:
ویژگی ها : اگه toggle button روشن کنی notifacition میاد و با اجبار هم میمونه اون بالا واگه خاموش کنی notification حذف میشه

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ToggleButton;


public class Notificatio extends Activity {
//private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
protected static final int NOTIFICATION_ID = 0;




@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("نوار اعلان");
setContentView(R.layout.notificition);


NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);


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


/* notificationID allows you to update the notification later on. */
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

/** تعریف تنظیمات برای نوتیفیکیشن ما */
/** تعریف تنظیمات برای نوتیفیکیشن ما */
int icon = R.drawable.ic_launcher;
/**آدرس آیکون موردنظر جهت نمایش در استاتوس بار*/
CharSequence tickerText = "نوتیفیکیشن فعال شد";
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "TagsUpLikes";
CharSequence contentText = "برای انتخاب تگ ها اینجا کلیک کنید";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);


/** مقداردهی اولیه Notificationبا استفاده از تنظیمات بالا */
final Notification notification = new Notification(icon, tickerText, when);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


/** بازیابی مرجع از NotificationManager*/
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);


final ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
tb.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if(tb.isChecked()){
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
else{
cancelNotification();

}
}
});


}

protected void cancelNotification() {
Log.i("Cancel", "notification");
mNotificationManager.cancel(NOTIFICATION_ID);






}


}

1yaram
پنج شنبه 09 مرداد 1393, 16:36 عصر
میشد اینکارو به شکل button انجام داد :

Button statusbarnotify = (Button) findViewById(R.id.Button1); statusbarnotify.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mNotificationManager.notify(NOTIFICATION_ID, notification);
}

});
Button cancelnorify = (Button) findViewById(R.id.button2);
cancelnorify.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
cancelNotification();

}
});
}