这是我的MyFirebaseMessagingService.class
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() { }
DataBaseUtil dataBaseUtil = null;
String sname;
Intent intent= new Intent();
String orderId;
String strmessage,strmsgtext;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
dataBaseUtil = new DataBaseUtil();
if (remoteMessage.getNotification() != null) {
L.e( "Message Notification Body: " + remoteMessage.getNotification().getBody());
RemoteMessage.Notification fcm = remoteMessage.getNotification();
PojoNotification pojo = new PojoNotification();
pojo.setTitle(fcm.getTitle());
pojo.setContent(fcm.getBody());
Map<String, String> dataMap = remoteMessage.getData();
orderId = dataMap.get("orderId").toString().trim();
sname = dataMap.get("screen_name").toString().trim();
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
pojo.setId(new Random().nextInt(9000));
dataBaseUtil.insertNotification(pojo);
if (SP.getBoolean(SP.LOGIN) ){
showNotification(pojo);
}
else {
}
}else {
L.e( "Message Notification Body: " + "abcv");
}
}
这是我的showNotification方法:
void showNotification(PojoNotification pojo) {
intent= new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("screen_name", sname);
// intent.putExtra(Constance.id,pojo.getId());
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
"SGrip",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DISCRIPTION");
mNotificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "default")
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(pojo.getTitle()) // title for notification
.setContentText(pojo.getContent())// message for notification
// .setSound(alarmSound) // set alarm sound for notification
.setAutoCancel(true); // clear notification after click
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
mNotificationManager.notify(0, mBuilder.build());
}
我在这里获取所有通知背景和前台,但是我面临的问题是,当我的应用程序在后台运行时,数据有效载荷数据无法获取,而当我的应用程序在前台运行时,所有数据都按需获取
在这里,我的启动器活动是我得到了额外的意图
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
这是通知代码服务器端
{
"registration_ids" : ["cFxiduH5j9I:APA91bEFAYPOR57yDga8urRuhmPuj_CI9h-bIyEwCcQeyFsvnFU_Nh9zkTmQVE7oiwdXchvIIz4DmUW1nqIOslBg_3oV7cWDZBjwb7WFqQ3E4RZ2T2vXCFN6IQ_1pBIfL67pHwthEZA4"],
"notification" : {
"title" : "First Notification",
"text": "Collapsing A",
"sound":"default"
},
"data" : {
"screen_name" : "acc_screen"
}
}
我阅读了许多教程,其中有很多关于stackoverflow的问答,但没有任何帮助。我知道有很多相同类型的问题,但是没有从那里得到解决方案,所以我发布了问题。
答案 0 :(得分:0)
当您的应用程序处于后台时,意图会被启动器活动捕获。
我在Kotlin中实现了相同的功能,请参阅:
val bundle = intent.extras
if (bundle != null) {
val notificationModel = NotificationModel()
try {
val target = Target()
if (bundle.containsKey("view"))
target.view = bundle.getString("view")
if (bundle.containsKey("circle_id"))
target.circleId = bundle.getString("circle_id")
if (bundle.containsKey("user_id"))
target.userId = bundle.getString("user_id")
if (bundle.containsKey("group_id"))
target.groupId = bundle.getString("group_id")
if (bundle.containsKey("file_id"))
target.fileId = bundle.getString("file_id")
notificationModel.target = target
} catch (ex: JSONException) {
ex.printStackTrace()
}
}
已更新
当您的应用程序在后台运行并单击通知时,将启动默认启动器。要启动所需的活动,您需要在通知有效负载中指定click_action。
$noti = array
(
'icon' => 'new',
'title' => 'title',
'body' => 'new msg',
'click_action' => 'your activity name comes here'
);
在您的android.manifest
文件中
在您注册活动的位置添加以下代码
<activity
android:name="your activity name">
<intent-filter>
<action android:name="your activity name" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>