티스토리 뷰

얼마전에 안드로이드 스튜디오를 업데이트 했더니 GCMService에서 Notification.setLatestEventInfo 부분에서 오류가 발생하더군요. 구글링 하여 문서를 찾아본 결과 CompleSdkVersion이 23이상일 경우 setLatestEventInfo 분이 삭제되어서 사용할 수 없게 되었다는 소식이 있었습니다. 그래서 해당 코드를 사용하지 않고 Notification Builder를 사용하여 동일한 Notification이 띄워지도록 하겠습니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
        String msg = intent.getStringExtra("msg");
        try {
            msg = URLDecoder.decode(msg, "EUC-KR");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.logo_small, "DSM 알리미 - 소식 왔어요!"System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE ;
        PendingIntent pendingIntent = PendingIntent.getActivity(this0new Intent(this, SplashActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this"DSM 알리미", msg, pendingIntent);
        nm.notify(1234, notification);
 
cs


기존의 위의 코드를 아래 코드와 같이 바꾸면 됩니다. 만약 컴파일 SDK 버전이 23미만 이면 위의 코드를 그대로 쓰셔도 되고, 23이상 이면 아래 코드를 사용해야 합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
        String msg = intent.getStringExtra("msg");
        try {
            msg = URLDecoder.decode(msg, "EUC-KR");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getActivity(this0new Intent(this, SplashActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
 
        Notification.Builder builder = new Notification.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.logo_small)
                .setContentTitle("DSM 알리미")
                .setContentText(msg)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setTicker("DSM 알리미 - 소식 왔어요!");
        Notification notification = builder.build();
        nm.notify(1234, notification);
cs




댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함