- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 fcm 发送带有数据有效负载的通知(我排除了通知对象,因为我希望用户无论应用程序处于前台/后台还是已被终止,都能收到通知)。
我能够获取通知并在用户点击通知时将用户导航到特定 Activity 。但是,我无法从 getIntent extras 中获取值。每次我尝试获取值时,我都会得到空值。我想不出哪里出了问题。
FCM 消息服务类
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");
}}
目标 Activity 类(匹配 intent 过滤器)
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());
}}
list
<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>
最佳答案
当您通过您创建的 Intent 发送数据时。而不是 getExtras
。试试 getStringExtra
或 getIntExtra
。因为 getExtras
只会在应用程序处于后台且 onMessageRecieved 未被调用时由 android 系统创建通知时为您提供数据。
关于android - getIntent.getExtras() 在通知点击时返回空数据 (Fcm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53780253/
您好,我正在尝试将 Activity 更改为 fragment ,以便将其添加到滑动布局中,我想我快完成了(除非任何人都可以在我的代码中看到任何其他错误)但我找不到在任何地方解决我的问题,它说'get
我有点困惑为什么在 RoutineRetrieved 函数中分配 ACTIVITYIMAGE 时使用 result.getInt(2) 并在分配 SLOT 时使用 result.getInt(3)..
我想从第二个 Activity 中获取一个值,以便在单独的类(fragmentPageAdapter)中使用它。首先,使用 Intent.putExtra(key_name, string) 将这个值
我有一个如下定义的 oracle 表: create table image_table_test (id number primary key, image ordsys.ordimage); 当我
/* * GETINTDRIVER -- program to call an integer reading function * * Compiling: put the getint fu
学习如何制作 Android 应用程序,我完成了这个教程。图的摘要在这里: http://sketchytech.blogspot.com/2012/10/android-simple-user-in
我是 android 应用程序的新手。我正在开发一个聊天应用程序。我使用了 RecyclerView 和内部 recyclerview 适配器,我在 Activity 开始时传递一个数据 下面是我的代
我正在通过这样的 Intent 从另一个 Activity 中获取 byteArray: if (view == null) { view = new ImageVie
我正在尝试将字符串从主要 Activity 传递到从服务扩展的另一个类。我从 main 的 onCreate 方法发送它: Intent phoneintent = new Intent(MainAc
我的程序由一个 MainActivity 和两个 fragment Activity 组成。我需要一个 fragment 从用户那里获取一个字符串值并将其传递给第二个 fragment 。 我正在努力
我不明白为什么我们使用方法 getIntent()。 因为,当我们需要那个方法时,我们可以使用方法onActivityResult()。 但是通过使用方法getIntent(),它可能会导致NullP
以下方法在 Activity 打开时返回空指针异常。有人知道为什么吗?调用 getIntent() 时是否与不存在 Intent 有关? 如有任何帮助,我们将不胜感激。 日志: 04-21 15:38
在 getView() 方法中,我想调用 getIntent()。在不开始新 Activity 的情况下如何实现这一目标。像这样的getView方法 public View getView(final
带你来看看mybatis为什么报"Invalid value for getInt()"这个错误。 背景 使用mybatis遇到一个非常奇葩的问题,错误如下: Cause: org.apache.ib
Firebase 提供以下方法: getBoolean() getByteArray() getDouble() getLong() getString() https://firebase.goog
此代码启动 Activity : Intent intent = new Intent(context, GameActivity.class); intent.putExtra("load"
我有一个问题,此代码仅返回默认值 -1。我尝试调试,它有值(value),所以我不知道为什么它总是返回-1。 private static final String KEY_CATEGORY_ID =
我在 app_restrictions.xml 中有这个: 现在,如果我想阅读它,我会得到 > java.lang.ClassCastException: java.lang.String cann
我有这个方法: public String givenCheckmark(String name, String date) { SQLiteDatabase db = Cache.open
在这里,resultSet.getInt() 不起作用,但我不知道我的代码出了什么问题。 我想增加列的值(名称为变量“出勤”)。使用 SELECT 语句我想读取当前值,通过使用 UPDATE 我想将相
我是一名优秀的程序员,十分优秀!