6

푸시 알림에 Google 클라우드 메시징 (GCM) 서비스를 사용하려면 첫 번째 Android 앱을 개발하고 있습니다. 필자는 서버 응용 프로그램에서 성공적으로 메시지를 보내고 클라이언트 응용 프로그램의 GCMIntentService 클래스에서 onMessage 이벤트에 메시지 내용을 기록 할 수있게되었습니다. 그러나 메시지가 수신되었다는 표시는 장치에 표시되지 않습니다. 나는 iPhone에서와 마찬가지로 전화로 풀다운 알림 목록에 메시지가 나타날 것으로 예상했다. 이 코드는 수동으로 코딩해야합니까? 또한 어떤 액티비티가 현재 활성화되어 있는지 여부와 앱이 백그라운드에서 유휴 상태인지 여부에 관계없이 메시지를 표시하는 일반적인 방법이 있습니까? 어떤 도움을 주셔서 감사합니다.Android - GCM 푸시 알림이 알림 목록에 표시되지 않음

답변

7

이 코드는 화면 상단의 Android 시스템 표시 줄에 알림을 생성합니다. 이 코드는 상단 바에서 알림을 클릭 한 후 사용자를 "Home.class"로 안내하는 새로운 의도를 만듭니다. 현재 활동을 기반으로 특정 작업을 수행하려면 GCMIntentService에서 다른 활동으로 브로드 캐스트 요청을 보낼 수 있습니다.

Intent notificationIntent=new Intent(context, Home.class); 
generateNotification(context, message, notificationIntent); 

private static void generateNotification(Context context, String message, Intent notificationIntent) { 
    int icon = R.drawable.icon; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String title = context.getString(R.string.app_name); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 
} 

이 예제에서는 작동해야하는 R.drawable 및 R.String의 리소스를 사용하지만 아이디어를 제공해야합니다. 상태 알림에 대한 자세한 내용은 http://developer.android.com/guide/topics/ui/notifiers/index.html 및 이에 대한 방송 수신자에 대한 정보를 참조하십시오. 당신이 GcmListenerService를 사용하는 경우 http://developer.android.com/reference/android/content/BroadcastReceiver.html

+0

에 추가,이 코드를 사용할 수 있습니다. –

+0

안녕하세요, 저는이 플러그인을 https://github.com/marknutter/GCM-Cordova에 사용하고 모든 지침을 따랐습니다. 그러나, 나는 애플 리케이션 내에서만 메시지를 얻을 수 있지만 안드로이드 알림 영역에 알림을 표시하는 응용 프로그램을 얻을 수 없습니다. 제발 조언. 나는이 플러그인을 사용하여 phonegap을 사용합니다. @Zachary Moshansky –

+0

죄송 합니다만 Phonegap을 사용한 적이 없으며 알림 기능을 사용하는 방법에 대해서는 언급 할 수 없습니다. –

1

당신은 환상적인 많은 감사의 당신의 onMessageReceived sendNotification에()

@Override 
public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     sendNotification(message); 
} 

private void sendNotification(String message) { 
     Intent intent = new Intent(this, YOURCLASS.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
     PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_park_notification) 
       .setContentTitle("Ppillo Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
관련 문제