티스토리 뷰

반응형

안녕하세요 오랜만에 글을 쓰네요


오늘의 포스팅은  Notification 을 사용할시에 알림이 하나오고 다시 오면 제거 되고 새로 갱신되는 않고 


이전에 있던걸 지워지지 않고 새로 갱신된게 위로 오는 방식을 알아볼꺼입니다.


제목은 저렇게 적었지만 상활을 적어야되는거라서 참 애매하네요 


일단 소프 보실께요






public class MyHandler extends NotificationsHandler {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;

@Override
public void onReceive(Context context, Bundle bundle) {
ctx = context;
String nhMessage = bundle.getString("message");
sendNotification(nhMessage);
if (MainActivity.isVisible) {
MainActivity.mainActivity.ToastNotify(nhMessage);
}
}

private void sendNotification(String msg) {

Intent intent = new Intent(ctx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

PendingIntent contentIntent = PendingIntent.getActivity(ctx, (int)(System.currentTimeMillis()/1000),
intent, PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
//bigTextStyle.setSummaryText("...");

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
//.setLargeIcon(R..ic_launcher)
.setContentTitle("Notification Hub Demo")
.setStyle(bigTextStyle
.bigText(msg))
.setSound(defaultSoundUri)
.setDefaults(Notification.FLAG_NO_CLEAR)
.setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build());
}

}






public class MyHandler extends NotificationsHandler {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;

@Override
public void onReceive(Context context, Bundle bundle) {
ctx = context;
String nhMessage = bundle.getString("message");
sendNotification(nhMessage);
if (MainActivity.isVisible) {
MainActivity.mainActivity.ToastNotify(nhMessage);
}
}

private void sendNotification(String msg) {

Intent intent = new Intent(ctx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

PendingIntent contentIntent = PendingIntent.getActivity(ctx, (int)(System.currentTimeMillis()/1000),
intent, PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
//bigTextStyle.setSummaryText("...");

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
//.setLargeIcon(R..ic_launcher)
.setContentTitle("Notification Hub Demo")
.setStyle(bigTextStyle
.bigText(msg))
.setSound(defaultSoundUri)
.setDefaults(Notification.FLAG_NO_CLEAR)
.setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build());
}
}

보시면 알겠지만 NotificationManager 에 request 값이 달라야지 Handler의 값을 다르게 인식함으로


이전 데이터를 지워지지 않게 되더라구요 


혹시 필요한분들 있을꺼 같아서 포스팅 합니다~~~



반응형
댓글