2016-11-16 91 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

你可以请你的雁 –

相关问题