您好我在这里使用Firebase Notification编写它,我使用onMessageReceived来获取通知,并将其发送到sendNotification功能,这里我的问题是,我的应用程序是睡眠模式,当我点击通知我需要去主活动页面,但我得到了错误,它从启动画面开始这是我在Firebase消息服务上的代码
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
String data;
String jsonObject;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom()); //MyFirebaseMsgService: From: 1055554437945
// Check if message contains a data payload.
data = String.valueOf(remoteMessage.getData());
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (remoteMessage.getData().size() > 0) {
jsonObject = remoteMessage.getData().toString();
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle()); ////MyFirebaseMsgService: Message Notification Body: Customer request for service
if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Service Request")) {
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getData().toString());
}
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody, String values) {
Log.i("messageBodymageBody", "" + messageBody);
if (isUserLoggedIn()) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("values", values);
intent.putExtra("identify", "1");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
startActivity(intent);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.fixmelogo)
.setContentTitle("Service Request")
.setContentText("Customer request for service")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
} else {
Intent intent1 = new Intent(this, LoginActivity.class);
startActivity(intent1);
}
}
public boolean isUserLoggedIn() {
Log.i("bollcheck", "" + UserInformations.getUserInformations(this).getId() + UserInformations.getUserInformations(this).getEmail());
/*if (UserInformations.getUserInformations(this).getId() != null || UserInformations.getUserInformations(this).getEmail() != null
|| UserInformations.getUserInformations(this).getMobile() != null) {*/
return UserInformations.getUserInformations(this).getId().trim().length() > 0;
}}
以及我的清单文件
<application
android:name=".App"
android:allowBackup="true"
android:icon="@drawable/fixmelogo"
android:label="@string/app_name"
android:roundIcon="@drawable/fixmelogo"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/fixmelogo" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimary" />
<activity android:name=".Activity.MainActivity"
android:exported="true"/>
<activity android:name=".Activity.TripActivity" />
</application>
当我点击通知需要主要活动时,但是它从启动画面页面开始,对不起我的英语不好,我是Android的初学者,请给出一些解决方案
嗨,这是我的服务器通知Json
{
"to":"d7XvGt8pD60:APA91bEjEe9IjhjJDmojvRDogMp1xc4sQFT9EcoB8TBvM-rCtkwryFhRVGvHIx1T6CWMoJu3l4UuaXgkgWrFj_Fo1SFhip9C-RXuthO6pfHvJ4GRQOipWtiVjUl9wtO7jfVd-T6jMBpJ",
"notification":{
"body":"Customer request for service",
"title":"Service Request",
"sound":"mySound"
},
"data":{
"CustomerId":"99",
"CustomerName":"Mayil",
"CustomerLastName":"Kannan",
"PhoneNumber":"96788484887",
"PickupLocation":"Ponmeni Muniyaandi koil main road, Chandragandhi Nagar, Ponmeni, Madurai, Tamil Nadu 625016",
"DropLocation":"Ponmeni Muniyaandi koil main road, Chandragandhi Nagar, Ponmeni, Madurai, Tamil Nadu 625016",
"PickupLatitude":"9.92074762937605",
"PickupLongitude":"78.0926296301913",
"DropLatitude":"9.92074762937605",
"DropLongitude":"78.0926296301913"
}
}
他们正在这样发送。