当我的应用程序未运行时,如何发送推送通知(后台/前台)

问题描述:

我想在用户连接互联网时尽快发送通知并显示通知;即使我的应用程序没有运行后台/前台。当我的应用程序未运行时,如何发送推送通知(后台/前台)

现在通知,只有当我的应用程序运行时显示...

<service android:name=".Services.MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 
    <service android:name=".Services.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 

    <activity android:name=".ReadNotification" /> 

这里是我的java类

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

    private static final String TAG = "Android News App"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     //It is optional 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 
     Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); 
    } 

    //This method is only generating push notification 
    private void sendNotification(String title, String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.icon64) 
       .setContentTitle(title) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
} 
+1

的可能的复制[如何处理通知,当应用程序在火力地堡背景](HTTPS: //*.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) –

您需要使用自定义的有效载荷为。例如

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "data" : { 
    "Nick" : "Mario", 
    "body" : "great match!", 
    "Room" : "PortugalVSDenmark" 
    }, 
} 

发送数据有效载荷看到here

也改变了方法来接收数据净荷

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()); 
      } 
     } 
    }