0

Android 기반의 firebase 푸시 알림에 대한 작업 중입니다. 성공적으로 구현되었습니다. 모두 잘 작동하고 있으며, 특정 액티비티가 푸시 탭에서 작동하지만 앱이 백그라운드에서 실행중인 경우 앱이 닫히면 (배경이 아님) 푸시를 두드리면 첫 번째 활동 (기본 활동)에서 앱이 시작됩니다. 행운을 빌어 많은 링크를 수색했습니다 , 그래서 어떤 친구도 알아낼 수 있도록 도와주세요, 여기에 제 코드를 게시하고 있습니다.앱이 닫힌 상태에서 푸시 알림 (백그라운드에서도 작동하지 않음)에서 작업 시작 방법

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 
    public static String PUSH_NOTIFICATION = "PUSH_NOTIFICATION"; 
    String store_id, img_url; 
    String store_name; 
    String headline; 
    String store_img; 
    int size = 200; 
    String subtext; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     size = (int) Math.ceil(Math.sqrt(300 * 200)); 
     Message message = new Message(); 
     message.obj = remoteMessage; 
     handler.sendMessage(message); 
     Intent pushNotification = new Intent(this.PUSH_NOTIFICATION); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 
    } 


    private Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
// TODO(developer): Handle FCM messages here. 
      Log.d(TAG, "=====push msg------by jigar--" + msg.toString()); 
      final RemoteMessage remoteMessage = (RemoteMessage) msg.obj; 
      if (remoteMessage.getData().size() > 0) { 
       Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
       final JSONObject jsonObject; 
       try { 
        jsonObject = new JSONObject(remoteMessage.getData().toString()); 
        Log.d(TAG, "tag: " + jsonObject.getString("store_id")); 
        Log.d(TAG, "message: " + jsonObject.getString("img_url")); 
        store_id = jsonObject.getString("store_id"); 
        img_url = jsonObject.getString("img_url"); 
        headline = jsonObject.getString("headline"); 
        store_name = jsonObject.getString("store_name"); 
        store_img = jsonObject.getString("store_image"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 

        Glide.with(getApplicationContext()) 
          .load(img_url) 
          .asBitmap() 
          .into(new SimpleTarget<Bitmap>() { 
           @Override 
           public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 
            ByteArrayOutputStream out = new ByteArrayOutputStream(); 
            resource.compress(Bitmap.CompressFormat.PNG, 100, out); 
            Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); 

            sendNotification(remoteMessage.getNotification().getBody(), decoded, store_id, remoteMessage.getNotification().getTitle()); 
           } 
          }); 
       } else { 
        sendNotification("", null, store_id, ""); 
       } 
      } 
     } 
    }; 

    private void sendNotification(String messageBody, Bitmap bitmap, String storeId, String subtext) { 
     Intent intent = null; 
     intent = new Intent(this, ProductDescriptionActivity.class); 
     intent.putExtra("storeid",store_id); 
     Const.STORE_ID = store_id; 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_UPDATE_CURRENT); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      Log.e("===BITMAP ", bitmap + ""); 
      NotificationCompat.BigPictureStyle notyStyle = new NotificationCompat.BigPictureStyle(); 
      notyStyle.bigPicture(bitmap); 
      mBuilder.setSmallIcon(R.drawable.ic_launcher) 
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
          R.drawable.ic_launcher)) 
        .setContentTitle(headline).setColor(getResources().getColor(R.color.orange)) 
        .setContentText(store_name + "\n" + messageBody).setContentInfo(subtext) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent) 
        .setStyle(notyStyle); 
     } else { 
      mBuilder.setSmallIcon(R.drawable.ic_launcher) 
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
          R.drawable.ic_launcher)) 
        .setContentTitle(headline) 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 
     } 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Random r = new Random(); 
     notificationManager.notify(r.nextInt(80 - 65) + 65, mBuilder.build()); 
     PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
     wl.acquire(6000); 
    } 
} 
+0

매니페스트 –

답변

0

AndroidManifest.xml 파일의 MainActivity 태그에 below 속성을 추가하십시오.

android:launchMode="singleTask" 
+0

mainActivity 또는 내가 시작하려는 활동을 게시 하시겠습니까? –

+0

예. 첫 번째 시작 활동. – Elango

+0

이것이 작동하지 않습니다. 아무것도 이전과 동일하지 않습니다., !! –

0

다음 코드를 사용하여 MainActivity를 열 수 있습니다.

NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent intent = PendingIntent.getActivity(context, 0, 
      notificationIntent, 0); 

    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 
+0

작동하지 않습니다. !!! –

+0

제 질문을 먼저 확인해주십시오. –