我在App上工作,我需要在其中显示通知.对于通知,我正在使用FireBase云消息传递(FCM).当app在后台时,我能够获得通知.
但是当我点击通知时,它会重定向到home.java页面.我希望它重定向到Notification.java页面.
所以,请告诉我如何在点击通知中指定活动.我使用两种服务:
1.)MyFirebaseMessagingService
2.)MyFirebaseInstanceIDService
这是我在MyFirebaseMessagingService类中的onMessageReceived()方法的代码示例.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;
public void onMessageReceived(RemoteMessage remoteMessage) {
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());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " …Run Code Online (Sandbox Code Playgroud) android android-notifications firebase-cloud-messaging firebase-notifications
我通过firebase控制台发送后没有收到通知,我尝试发送了很多通知,但收到了大约20个通知中的一两个,我按照github的指南firebase消息 ,为什么我没有收到通知,我的应用程序安装在一个模拟器中并且在我的一部电话中但是当我收到通过通知面板发送的通知时,我通过电话或模拟器得到它们从来没有得到它们.
我在Android应用中实施了Firebase推送通知.我实现了两个服务来注册令牌,并在检测到时发出通知.当我的应用程序启动时它正在工作,但当我的应用程序关闭时,它无法正常工作.
public class FirebaseinstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("Firebase", refreshedToken);
MainActivity.setFirebaseToken(refreshedToken);
}
}
public class MyFirebaseMessageService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Notification Message Body: " + remoteMessage.getData().get("title"));
//Calling method to generate notification
sendNotification(remoteMessage.getData());
}
//This method is only generating push notification
//It is same as we did in …Run Code Online (Sandbox Code Playgroud) 我已经尝试了这3天的android通知工作,用户点击通知然后活动打开.但每次我举杯,它都说是空的.尝试使用SOF的一些解决方案,但不起作用.你能看看代码有什么问题吗?提前致谢.
通知代码是
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
int notificationId = new Random().nextInt();
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext(),"Default");
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("fromNotification", true);
// Sets the Activity to start in a new, empty task …Run Code Online (Sandbox Code Playgroud) 我使用firebase来构建我的项目.
它还将使用FCM(firebase云消息).
但有一个问题.
当app在后台时,我无法处理FCM(创建我的自定义通知).
官方网站教程说
案例1:应用前台 - >覆盖"onMessageReceived()"来创建自定义通知.
案例2:应用程序背景 - >系统将直接创建通知.我们不需要也不能做任何事情.因为在这种情况下它不会触发"onMessageReceived()".
但是,如果我在应用程序是后台时无法执行任何操作,则无法创建自定义通知.(例如,在用户单击通知后,它将弹出一个窗口以显示详细信息.)
那么,当应用程序处于后台时,如何使用FCM处理通知?
我通过使用以下代码为推送通知设置大图标。
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.green))
.setSound(defaultSoundUri)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,并largeIcon在应用程序位于前景中时显示,但在应用程序不在前景中时,它不会显示大图标。
我正在测试应用程序 samsung s7(oreo)
我正在使用Firebase云消息传递API创建应用程序...我能够从服务器向我的客户端应用程序发送通知和数据.但问题是当应用程序打开时,通知不会在数据出现时触发(我的意思是我记录了它)这不是问题.但是当应用程序关闭时,会收到通知,而我点击通知时会打开活动,而我无法看到日志数据.我需要将数据更新为TextView ..
我的MyFirebaseMessagingService:
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Log.e("FROM", "From: " + remoteMessage.getFrom());
String data = remoteMessage.getData().get("message");
Log.e("KOIII", "Notification Message Body: …Run Code Online (Sandbox Code Playgroud) 我已在我的应用程序中实施了FCM Firebase云消息传递.当应用程序处于forground模式时,我收到通知及其数据.
问题:
当我的应用程序处于后台模式或当时被杀死时,通知会通过FCM自动生成.所以我无法控制到达的通知数据(通知的coustom数据字段).当应用程序处于后台模式并且通知到达时,MyFirebaseMessagingService类的onMessageReceived(RemoteMessage remoteMessage)方法不会调用.
请帮助在两种模式下获得通知控制(与GCM相同)