0

알림을 클릭 한 후 ShowNotificationActivity를 리디렉션하고 싶습니다. 하지만 그것을 클릭하면 앱이 처음부터 시작됩니다. 이 문제를 해결하는 방법? 여기 코드가 있습니다. 다른 클래스에서 알림을 클릭 한 후 특정 활동을 여는 방법은 무엇입니까?

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); 

    private NotificationUtils notificationUtils; 
    NotificationDatabaseHandler notificationDatabaseHandler; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.e(TAG, "From: " + remoteMessage.getFrom()); 

    if (remoteMessage == null) 
     return; 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
     handleNotification(remoteMessage.getNotification().getBody()); 
    } 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

     try { 
      JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
      handleDataMessage(json); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
    } 
    } 

    private void handleNotification(String message) { 
    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
     // app is in foreground, broadcast the push message 
     Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
     pushNotification.putExtra("message", message); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

     // play notification sound 
     NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
     notificationUtils.playNotificationSound(); 
    }else{ 
     // If the app is in background, firebase itself handles the notification 
    } 
    } 

    private void handleDataMessage(JSONObject json) { 
    Log.e(TAG, "push json: " + json.toString()); 

    try { 
     JSONObject data = json.getJSONObject("data"); 

     String title = data.getString("title"); 
     String message = data.getString("message"); 

     boolean isBackground = data.getBoolean("is_background"); 
     String imageUrl = data.getString("image"); 
     String timestamp = data.getString("timestamp"); 
     JSONObject payload = data.getJSONObject("payload"); 

     Log.e(TAG, "title: " + title); 
     Log.e("yes", "message: " + message); 
     Log.e(TAG, "isBackground: " + isBackground); 
     Log.e(TAG, "payload: " + payload.toString()); 
     Log.e(TAG, "imageUrl: " + imageUrl); 
     Log.e(TAG, "timestamp: " + timestamp); 

     if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
      // app is in foreground, broadcast the push message 
      Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
      pushNotification.putExtra("message", message); 
      LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

      // play notification sound 
      NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
      notificationUtils.playNotificationSound(); 

     } else { 
      // app is in background, show the notification in notification tray 
      Intent resultIntent = new Intent(getApplicationContext(), ShowNotificationActivity.class); 
      resultIntent.putExtra("message", message); 


      // check for image attachment 
      if (TextUtils.isEmpty(imageUrl)) { 
       showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent); 
      } else { 
       // image is present, show notification with image 
       showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl); 
      } 
     } 
    } catch (JSONException e) { 
     Log.e(TAG, "Json Exception: " + e.getMessage()); 
    } catch (Exception e) { 
     Log.e(TAG, "Exception: " + e.getMessage()); 
    } 
    } 

    /** 
    * Showing notification with text only 
    */ 
    private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { 
    notificationUtils = new NotificationUtils(context); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent); 
    } 

    /** 
    * Showing notification with text and image 
    */ 
    private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { 
    notificationUtils = new NotificationUtils(context); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl); 
    } 
} 

.........................

public void showNotificationMessage(String title, String message, String timeStamp, Intent intent) { 
    showNotificationMessage(title, message, timeStamp, intent, null); 
} 

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) { 
    // Check for empty push message 
    if (TextUtils.isEmpty(message)) 
     return; 

    // notification icon 
    final int icon = R.drawable.ic_notification; 

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    final PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        mContext, 
        0, 
        intent, 
        PendingIntent.FLAG_CANCEL_CURRENT 
      ); 
} 
+0

사용하면 알림을 클릭하면 'PendingIntent' 특정 활동을 엽니 다. http://stackoverflow.com/questions/20881226/pending-intent-in-notification-not-working – Shailesh

답변

0

는 SURAJ 헤이 간단

그냥 추가하십시오

resultIntent = (getApplicationContext(), SecondActivity.class); 

알림 클릭으로 전화를 걸고 자하는 활동입니다.

그리고 끝났습니다. 다음과 같은 알림 사용을 보낼 때

+0

예 sir handleDataMessage() 메소드에서 이미 수행했습니다. –

0

는 :

NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(mContext) 
       .....; 


Intent activityIntent = new Intent(mContext, ShowNotificationActivity.class); 
      activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, activityIntent, PendingIntent.FLAG_ONE_SHOT); 
     mBuilder.setContentIntent(contentIntent); 

     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
관련 문제