0

AWS SNS를 사용하여 SNS 항목과 Device Endpoint 모두에 푸시 알림을 보내고 있습니다. 앱이 실행 중이 아니지만 앱이 실행되는 동안 알림을 표시하지 않는 경우 알림이 표시됩니다.내 Android 앱이 실행 중이더라도 알림을 표시하려고합니다.

알림은 앱 사용자에게 앱의 다른 섹션에서 발생하는 변경 사항을 알려주기 때문에 알림이 표시되므로 앱 준비가되면 체크 아웃 할 수 있습니다. 다른 사람에게 메시지를 입력하는 동안 다른 그룹 채팅에서 새로운 Whatsapp 메시지 알림을받는 것과 같은 것입니다.

다음은 알림 빌더 코드입니다. 모든 힌트는 정말 감사하겠습니다. 감사.

private void displayNotification(final String message) { 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    int requestID = (int) System.currentTimeMillis(); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    Bitmap large_icon = BitmapFactory.decodeResource(this.getResources(), 
      R.mipmap.myImage); 

    // Display a notification with an icon, message as content, and default sound. It also 
    // opens the app when the notification is clicked. 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setLargeIcon(large_icon) 
      .setContentTitle("My Subject Title") 
      .setContentText(message) 
      .setDefaults(Notification.DEFAULT_SOUND) 
      .setAutoCancel(true) 
      .setContentIntent(contentIntent); 

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

    notificationManager.notify(0, builder.build()); 
} 
+0

안녕하세요, 사용자가 앱의 알림 센터에서 알림을 클릭하면이 메소드가 호출됩니다. –

+0

아이콘, 내용으로 메시지 및 기본 소리로 알림을 표시합니다. 또한 알림을 클릭하면 앱이 열립니다. –

+0

그래서 나는 인앱 알림을 받기 위해서는 BroadcastReceiver를 사용하는 것이고 수신 알림을 구독하고 앱이 실행되는 동안 처리 할 수 ​​있다고 생각합니다.이 가이드는 http : //www.androidbegin을 따라갈 수 있습니다. co.kr/tutorial/android-broadcast-receiver-notification-tutorial/ –

답변

0

알림 메시지를 표시하려면 아래 코드를 사용하십시오.

private static void generateNotification(Context context, final String message) 
{ 
    try { 
     NotificationManager mNotificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     //Define sound URI 
      Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      Intent notificationIntent = new Intent(context, DemoActivity.class); 
     notificationIntent.putExtra("message", message); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent contentIntent = PendingIntent.getActivity(context, RandomNumberGenerator.getRandom(),notificationIntent 
       /*new Intent(context, LuncherActivity.class)*/, 0); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context); 
     mBuilder.setSmallIcon(R.drawable.icon_small); 
     mBuilder.setContentTitle("YOUR-APP-NAME"); 
     // mBuilder.setStyle(new NotificationCompat.BigTextStyle() 
     // mBuilder.bigText(msg); 
     mBuilder.setContentText(message); 
     mBuilder.setAutoCancel(true); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setSound(soundUri); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
     } 

    } 

공용 클래스 RandomNumberGenerator {

public static int getRandom() 
{ 
    int random_number=0;  
    Random random = new Random(); 
    random_number= random.nextInt(9999 - 1000) + 1000; 
    return random_number; 
    } 

} 

가 내 경우에는 작업 know.its을 시도하자 아래와 같이 임의의 숫자 genetor 클래스 만들기 .

+0

이 제안에 감사드립니다. 시간과 응답을 감사하십시오. –

0

문제점을 발견했습니다.

@Override 
public void onMessageReceived(final String from, final Bundle data) { 
    String message = getMessage(data); 
    Log.d(LOG_TAG, "From: " + from); 
    Log.d(LOG_TAG, "Message: " + message); 
    // Display a notification in the notification center if the app is in the background. 
    // Otherwise, send a local broadcast to the app and let the app handle it. 
    displayNotification(message); 

    /* if (isForeground(this)) { 
     // broadcast notification 
     broadcast(from, data); 
    } else { 
     displayNotification(message); 
    } */ 
} 

내 displayNotification() 함수는 때문에 "만약"조건문의 호출되지 않은 그 것이다 응용 프로그램이 포 그라운드에 있지 만 표시 통지합니다. 이제는 onMessageReceived()가 메시지를 받으면 내 응용 프로그램이 현재 실행 중이더라도 직접 displayNotification()에 전달합니다. 이는 원하는 것입니다.

관련 문제