2016-11-16 93 views
0

我遇到了有關通知的問題。我的應用中有一項服務,用於偵聽來自Firebase的郵件。這裏是我的代碼:當應用程序處於後臺時,通知具有不同的行爲

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // super.onMessageReceived(remoteMessage); 

    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    displayNotification(remoteMessage.getNotification().getBody()); 
} 


private void displayNotification(String message) { 
if (message.contains("upgrade")) { 
     upgradeClient(message); 
    } 

} 

private void upgradeClient(String message) { 

    final String appPackageName = getString(R.string.app_package); 
    String url; 
    try { 
     url = "market://details?id=" + appPackageName; 
    } catch (final Exception e) { 
     url = "https://play.google.com/store/apps/details?id=" + appPackageName; 
    } 

    //Open the app page in Google Play store: 
    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 


    Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle(getResources().getString(R.string.app_name)) 
      .setContentText(message) 
      .setSmallIcon(R.drawable.ic_local_notification) 
      .setSound(notificationSound) 
      .setContentIntent(pendingIntent); 

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0, builder.build()); 

} 

}

即使應用前景化,通知不正是它應該做的(在這種情況下,將用戶帶到Play商店)。問題是當應用程序背景時。在應用程序處於後臺時點擊通知會導致應用程序啓動,並且不會讓用戶玩商店。 upgradeClient()甚至沒有被調用!先謝謝你!

+0

檢查您是否正在弄髒 –

+0

嗨jitesh mohite。你是對的,我沒有得到消息。請發表您的評論作爲答案,我會接受它。謝謝隊友 –

+0

完成。感謝它 –

回答

1

如果您正在使用服務請檢查消息

字符串消息可能爲空。

private void displayNotification(String message) { 
if (message.contains("upgrade")) { 
     upgradeClient(message); 
    } 

} 
+0

你可以請你的雁 –

相關問題