可以,你只需要知道推送通知如何在Android火力點工作。
你需要重寫
handleIntent功能。
此函數在後臺處理Firebase通知。因此,在它內部,您將使您的推送通知以推送消息的形式發送所有數據。不要忘記從消息中提取信息。您可以使用標題或正文等默認空格,但也可以發送一些自定義數據。
接下來我會附上一個示例代碼,它是如何工作的。
注意:如果你還沒有這個方法,那麼你需要升級火力版本比起來10.0.1
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
@Override
public void handleIntent(Intent intent) {
//super.handleIntent(intent);
Log.d(TAG,"Handling Intent");
Bundle mBundle = intent.getExtras();
String img = mBundle.getString("imgURL");
String title = mBundle.getString("gcm.notification.title");
String body = mBundle.getString("gcm.notification.body");
mBundle.putInt("promoId",Integer.valueOf(mBundle.getString("promoId")));
Integer id = mBundle.getInt("promoId");
sendNotification(mBundle);
}
private void sendNotification(Bundle mBundle) {
// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.putExtras(mBundle);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
String title = mBundle.getString("gcm.notification.title");
String body = mBundle.getString("gcm.notification.body");
// Define the notification settings.
builder.setSmallIcon(R.mipmap.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setColor(Color.RED)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
}
,如果您有任何問題,請與我將編輯英里的答案。
在後臺它會直接觸發,你不能改變消息。您需要在服務器端的通知有效負載中設置消息。當應用程序處於後臺時,您可以使用來自主要活動或操作活動的意向束來處理負載。 –
查看[如何處理Firebase中後臺應用的通知](http://stackoverflow.com/a/37845174/3536264) –
從哪裏發送通知?我的意思是從Firebase控制檯或你的服務器?如果您從Firebase發送信息,則在發送通知時,轉到高級選項並向通知添加數據。如果您從服務器發送,則不發送顯示消息(僅發送數據消息)。 –