Android getIntent.getExtras()在通知单击时返回空数据(Fcm)
我使用fcm发送带有数据有效负载的通知(我不包括通知对象,因为我希望用户获得通知,无论应用程序是在前台/后台还是被杀死) 我能够获取通知,并在单击通知时将用户导航到特定活动。但是,我无法从getIntent extras获取值。每次我尝试获取值时,都会得到空值。我想不出哪里出了错 FCM消息服务类Android getIntent.getExtras()在通知单击时返回空数据(Fcm),android,firebase,push-notification,firebase-cloud-messaging,android-push-notification,Android,Firebase,Push Notification,Firebase Cloud Messaging,Android Push Notification,我使用fcm发送带有数据有效负载的通知(我不包括通知对象,因为我希望用户获得通知,无论应用程序是在前台/后台还是被杀死) 我能够获取通知,并在单击通知时将用户导航到特定活动。但是,我无法从getIntent extras获取值。每次我尝试获取值时,都会得到空值。我想不出哪里出了错 FCM消息服务类 public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final Str
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingServ";
NotificationManager notificationManager;
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.e(TAG, "onNewToken: "+token );
if(PrefManager.isVendorLoggedIn(MyFirebaseMessagingService.this))
sendNewTokenToServer(token);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage);
}
private void showNotification(RemoteMessage remoteMessage){
String orderID="";
Map<String,String> dataMap = remoteMessage.getData();
orderID = dataMap.get("order_id");
String title="New Order";
String message="Click here to view the Details";
String click_action=dataMap.get("click_action");
Intent intent=new Intent(click_action);
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
intent.putExtra(StringConstants.CURRENT_ORDER_ID,orderID);
intent.putExtra(StringConstants.BUZ_ID,dataMap.get("business_id"));
intent.putExtra("TITLE",title);
intent.putExtra("BODY",message);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.drawable.splash_logo);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(8,notificationBuilder.build());
}
private void sendNoti(RemoteMessage remoteMessage){
String click_action=remoteMessage.getData().get("click_action");
Intent intent=new Intent(click_action);
//Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_UPDATE_CURRENT);
String channelId = "101";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.splash_logo)
.setContentTitle("New Order receieved!!")
.setContentText("Click to view details")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
private void sendNewTokenToServer(String token){
PrefManager prefManager = new PrefManager(MyFirebaseMessagingService.this);
String url = Constants.BASE_URL+"vendor/add-refresh-token";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("vendor_id",prefManager.getVendorId(getApplicationContext()));
jsonObject.put("token",token);
} catch (JSONException e) {
e.printStackTrace();
}
CustomJsonRequest customJsonRequest = new CustomJsonRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
customJsonRequest.setPriority(Request.Priority.HIGH);
ReatchAll helper = ReatchAll.getInstance();
helper.addToRequestQueue(customJsonRequest,"UPDATE_TOKEN");
}}
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
私有静态最终字符串TAG=“MyFirebaseMessagingServ”;
通知经理通知经理;
@凌驾
公共void onNewToken(字符串标记){
super.onNewToken(代币);
Log.e(标记“onNewToken:+令牌”);
if(PrefManager.isVendorLoggedIn(MyFirebaseMessagingService.this))
sendNewTokenToServer(令牌);
}
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
super.onMessageReceived(remoteMessage);
显示通知(远程消息);
}
私有无效显示通知(RemoteMessage RemoteMessage){
字符串orderID=“”;
Map dataMap=remoteMessage.getData();
orderID=dataMap.get(“order_id”);
String title=“新订单”;
String message=“单击此处查看详细信息”;
String click_action=dataMap.get(“click_action”);
意图=新意图(单击操作);
intent.addFlags(intent.FLAG\u活动\u新任务);
intent.addFlags(intent.FLAG\u活动\u清除\u任务);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);
intent.putExtra(StringConstants.CURRENT\u ORDER\u ID,orderID);
intent.putExtra(StringConstants.BUZ_ID,dataMap.get(“business_ID”));
意向。额外(“标题”,标题);
意图。putExtra(“正文”,信息);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此);
notificationBuilder.setContentTitle(标题);
notificationBuilder.setContentText(消息);
notificationBuilder.setSmallIcon(R.drawable.splash_徽标);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(待定内容);
notificationManager=(notificationManager)getSystemService(Context.NOTIFICATION\u服务);
notificationManager.notify(8,notificationBuilder.build());
}
私有void sendNoti(RemoteMessage RemoteMessage){
String click_action=remoteMessage.getData().get(“click_action”);
意图=新意图(单击操作);
//意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,意图,
PendingEvent.FLAG_ONE_SHOT | PendingEvent.FLAG_UPDATE_CURRENT);
字符串channelId=“101”;
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=
新建NotificationCompat.Builder(此,channelId)
.setSmallIcon(可拉深的R.splash_标志)
.setContentTitle(“收到新订单!!”)
.setContentText(“单击以查看详细信息”)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
//因为需要android Oreo通知通道。
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
NotificationChannel=新的NotificationChannel(信道ID,
“频道可读标题”,
NotificationManager.重要性(默认值);
notificationManager.createNotificationChannel(频道);
}
notificationManager.notify(通知的0/*ID*/,notificationBuilder.build());
}
私有void sendnetokentoserver(字符串令牌){
PrefManager PrefManager=新的PrefManager(MyFirebaseMessagingService.this);
字符串url=常量。基本url+“供应商/添加刷新令牌”;
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject.put(“供应商id”,prefManager.getVendorId(getApplicationContext());
jsonObject.put(“令牌”,令牌);
}捕获(JSONException e){
e、 printStackTrace();
}
CustomJsonRequest CustomJsonRequest=new CustomJsonRequest(Request.Method.POST,url,jsonObject,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
customJsonRequest.setPriority(Request.Priority.HIGH);
ReatchAll helper=ReatchAll.getInstance();
addToRequestQueue(customJsonRequest,“更新令牌”);
}}
目标活动类(与目标筛选器匹配)
公共类供应商currentorderActivity扩展了AppCompative活动{
私有静态最终字符串TAG=“VendorCurrentOrderActiv”;
语境;
ReatchAll helper=ReatchAll.getInstance();
CustomProgressDialog CustomProgressDialog;
PrefManager PrefManager;
字符串orderId,buzId;
OrderedItemsAdapter OrderedItemsAdapter;
ArrayList OrderEditeArrayList;
回收视图项srcv;
图像视图反箭头;
FontTextView接受订单、拒绝订单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u vendor\u current\u order);
context=VendorCurrentOrderActivity.this;
customProgressDialog=新建customProgressDialog(上下文);
prefManager=新的prefManager(上下文);
backArrow=(ImageView)findViewById(R.id.back\u arrow);
backArrow.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
菲尼
public class VendorCurrentOrderActivity extends AppCompatActivity {
private static final String TAG = "VendorCurrentOrderActiv";
Context context;
ReatchAll helper = ReatchAll.getInstance();
CustomProgressDialog customProgressDialog;
PrefManager prefManager;
String orderId,buzId;
OrderedItemsAdapter orderedItemsAdapter;
ArrayList<OrderedItem> orderedItemArrayList;
RecyclerView itemsRcv;
ImageView backArrow;
FontTextView acceptOrder,rejectOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vendor_current_order);
context = VendorCurrentOrderActivity.this;
customProgressDialog = new CustomProgressDialog(context);
prefManager = new PrefManager(context);
backArrow =(ImageView)findViewById(R.id.back_arrow);
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
initViews();
orderId = getIntent().getExtras().getString(StringConstants.CURRENT_ORDER_ID);
buzId = getIntent().getExtras().getString(StringConstants.BUZ_ID);
Log.e(TAG, "onCreate: "+orderId);
Log.e(TAG, "onCreate: NOTI DATA "+getIntent().getExtras().getString("TITLE")+" "+getIntent().getExtras().getString("BODY") );
customProgressDialog.showDialog();
getOrderDetails();
// onNewIntent(getIntent());
}}
<activity android:name=".Vendor.Orders.VendorCurrentOrderActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
android:exported="true">
<intent-filter>
<action android:name="NEW_ORDER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseMessagingService ">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>