单击firebase通知时的java打开通知活动
我希望通知打开通知。类活动而不是MainActivity。我已将代码更改为:
Intent intent = new Intent(this, Notification.class);
但当收到通知并单击它时,仍然会打开MainActivity。 我查看了很多示例,但无法找到需要更改的位置以获得另一个活动,而不是MainActivity
通知作为用户段从firebase控制台发送
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.graphics.Color;
import 安卓.media.RingtoneManager;
import 安卓.net.Uri;
import 安卓.support.v4.app.NotificationCompat;
import 安卓.util.Log;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
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);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_bg)
.setContentTitle("FCM Message")
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
# 1 楼答案
可能是这样,改变意图:
因此,为了澄清,firebase控制台中的您不能仅从API打开MainActivity之外的其他活动
为什么:
因为两件事
一个:
从控制台发送的通知不会激发onMessageReceived()
两个:
firebase cosole通知无法设置单击操作
修复方法:
从API发送通知并设置单击操作
例如:
要设置请单击u操作,在安卓清单中,在通知活动中设置此项:
# 2 楼答案
# 3 楼答案
修改
sendNotifcation
方法中的代码。正如你所看到的,你写的是MainAcitivity.class
,而不是NotificationActivity.class
。我猜这是个打字错误# 4 楼答案
在您提供的代码中,我没有找到对
sendNotification()
的调用。我很确定你看到的通知是来自Firebase而不是来自sendNotification()
。见this page。如果您的应用程序位于后台,并且通知有效负载包含通知部分,那么您的通知将被传递到系统托盘,系统托盘在单击时启动启动器活动,并以捆绑的形式将任何数据从通知传递到启动器活动要解决您的问题,请只发送数据消息,并在
onMessageReceived()
的适当位置呼叫sendNotification()
,以便Notification.class