![](/img/trans.png)
[英]FCM notification, notification click when app is in foreground not background
[英]FCM : Handling different data notification of an android app when app is in foreground
我正在使用雲功能在我的應用程序中發送2種不同的數據通知。
當我在應用程序中收到第一條通知並點擊通知時,它會使用未決Intent重定向到我的預期頁面。
現在,當我在應用程序中收到第二條通知並點擊通知時,如何重定向到另一個活動(已發送邀請頁面)。 如何實現這一點。
我的firebasemessaging類
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.HashMap;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MY android fcm service";
FirebaseUser firebaseUser;
FirebaseAuth mAuth;
FirebaseFirestore db = FirebaseFirestore.getInstance();
String docId ;
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
String FirstName = "0";
String Amount = "0";
String SenderID1 = "0";
String ReceiverId1 ="0";
String ReceiverPhoneNumberst="0";
String SenderAutoId="0";
mAuth = FirebaseAuth.getInstance();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
FirstName = remoteMessage.getData().get("Name");
Amount = remoteMessage.getData().get("Amount");
SenderID1=remoteMessage.getData().get("SenderID");
System.out.println("Senderid"+SenderID1);
ReceiverId1=remoteMessage.getData().get("ReceiverId");
ReceiverPhoneNumberst=remoteMessage.getData().get("ReceiverPhoneNumber");
long ReceiverPhoneNumber = Long.parseLong(ReceiverPhoneNumberst);
SenderAutoId=remoteMessage.getData().get("SenderAutoId");
Map<String,Object> data = new HashMap<>();
data.put("SenderName",FirstName);
data.put("Amount",Amount);
data.put("SenderID",SenderID1);
data.put("ReceiverId",ReceiverId1);
data.put("ReceiverPhoneNumber",ReceiverPhoneNumber);
data.put("SenderAutoId",SenderAutoId);
final String finalAmount = Amount;
final String finalFirstName = FirstName;
db.collection("deyaPayUsers").document(mAuth.getUid().toString()).collection("Split").document(mAuth.getUid().toString()).collection("ReceivedInvitation").add(data).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
docId = documentReference.getId();
Log.d(TAG,"DocumentSnapshot written with ID:" +docId);
//System.out.println("documentadded is docId"+ docId);
Log.d(TAG, "From:" + remoteMessage.getFrom());
Log.d(TAG, "Notification MessageBody:" + remoteMessage.getNotification().getBody());
Log.d(TAG, "Notification data:" + remoteMessage.getData());
createNotification(remoteMessage.getNotification().getBody(), finalFirstName, finalAmount, docId);
// createNotification1(remoteMessage.getNotification().getBody());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG,"Error adding document",e);
}
});
}
}
private void createNotification(String messageBody, String FirstName, String amount,String docId) {
Intent intent = new Intent( this,ReceivedNotification.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("FirstName",FirstName);
System.out.println(FirstName);
intent.putExtra("Amount",amount);
intent.putExtra("DocumentId",docId);
System.out.print("dDA "+docId);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("deyaPay")
.setContentText(messageBody)
.setAutoCancel(true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent)
.setVisibility(Notification.VISIBILITY_PUBLIC);
//.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
//.addAction(R.drawable.accept, "Accept",) // #0
//.addAction(R.drawable.ic_reject, "Reject", ResultActivity.class) ; // ;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
這是您從服務器推送的通知中使用標記的簡單代碼段。
當您在onMessageRecevied函數中收到通知時,提取該標志並將其傳遞給sendNotification
private void sendNotification(int flag, String title,String messageBody ) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long notificatioId = System.currentTimeMillis();
Intent intent=null;
if(flag==1){
intent = new Intent(getApplicationContext(), activity1.class);
}
else{
intent = new Intent(getApplicationContext(), activity2.class);
}
intent.putExtra("fromItem",false);
intent.putExtra("title", title);; // Here pass your activity where you want to redirect.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
currentapiVersion = R.mipmap.ic_launcher;
} else{
currentapiVersion = R.mipmap.ic_launcher;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(currentapiVersion)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentText(title)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
}
在服務器端代碼中,將標志添加到JSON消息中
var data = new
{
to=deviceId,
notification = new
{
body = "\""+notificationMessage +"\"",
title = "\""+_title+"\"",
sound = "Enabled\""
}
,
data=new {
flag= "\"" + _flag+"\"",
message= "\""+notificationMessage+"\"",
additional = "\"" + addtional+"\""
}
};
這是json數據包的簡單代碼段
您無法在NotificationBuilder
預定義它,但是您可以做一件事,創建一個通用活動,並使用某種標記/類型將所有通知路由到該活動,以識別通知類型。 通過檢查您傳遞的通知類型,您可以從此活動重定向到所需的活動。
在此函數內,根據messageBody更改Intent,
private void createNotification(String messageBody, String FirstName, String amount,String docId) {
Intent intent=null;
if(messageBody.contains(“xyz”))
intent = new Intent( this, ReceivedNotification.class );
else
intent = new Intent( this, SecondReceivedNotification.class );
— — —
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
並將其傳遞給“待處理的意圖”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.