Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MyFirebaseMessagingService.onMessageReceived()不工作_Android_Firebase_Firebase Cloud Messaging_Progressive Web Apps_Trusted Web Activity - Fatal编程技术网

Android MyFirebaseMessagingService.onMessageReceived()不工作

Android MyFirebaseMessagingService.onMessageReceived()不工作,android,firebase,firebase-cloud-messaging,progressive-web-apps,trusted-web-activity,Android,Firebase,Firebase Cloud Messaging,Progressive Web Apps,Trusted Web Activity,我正在尝试“捕获”发送到应用程序的firebase云消息,当我收到(/单击)一条消息时,我希望根据firebase控制台中添加的“自定义数据”键/值执行一些操作 我现在的代码如下所示: MyFirebaseMessagingService: package com.company.app; import android.util.Log; import androidx.annotation.NonNull; import com.google.firebase.messaging.Fir

我正在尝试“捕获”发送到应用程序的firebase云消息,当我收到(/单击)一条消息时,我希望根据firebase控制台中添加的“自定义数据”键/值执行一些操作

我现在的代码如下所示:

MyFirebaseMessagingService:

package com.company.app;

import android.util.Log;

import androidx.annotation.NonNull;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "Notification";

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.i(TAG, "Receiving notification...");
        for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            Log.d(TAG, "key, " + key + " value " + value);
        }
    }
}
        ...

        <!-- Firebase notification -->
        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">

            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

    </application>
dependencies {

    //Google firebase
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.google.firebase:firebase-messaging:20.0.1'

    ...

我猜
没有被执行,但我不知道为什么。 我没有得到任何错误。 除此之外,我的通知工作没有任何问题

这是控制台日志,如果有任何帮助:

D/FA: Logging event (FE): notification_receive(_nr), 
Bundle[{
 ga_event_origin(_o)=fcm,
 message_device_time(_ndt)=0,
 message_type(_nmc)=display,
 message_name(_nmn)=B1 - TN18,
 message_time(_nmt)=1574177464,
 message_id(_nmid)=8950284200210511319}]
有什么想法吗

编辑:相同的代码在其他空应用程序中工作

编辑2:这个问题不知何故是由谷歌的TWA活动引起的

编辑3:

public class LauncherActivityTWA extends AppCompatActivity {

private TwaLauncher mTwaLauncher;
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    this.mTwaLauncher.launch(getLaunchingUrl()); //this is causing the issue
}

编辑4:现在我已经通过切换到WebView解决了这个问题,稍后我将尝试使用TWA。我仍然认为这个问题是由TWA或与之相关的一些图书馆引起的

编辑5:所以我找到了一种解决方法——如果你想同时使用TWA和FCM,你只需要处理数据消息,即使应用程序在前台,通知消息似乎也不会触发消息接收

@覆盖
收到消息时公共无效(RemoteMessage RemoteMessage){
if(remoteMessage==null)
返回;
//检查消息是否包含通知负载。
if(remoteMessage.getNotification()!=null){
handleNotification(remoteMessage.getNotification().getBody());
}
//检查消息是否包含数据有效负载。
如果(remoteMessage.getData().size()>0){
字符串标题=”,正文=”,类型=”;
Intent hIntent=新的意图(getApplicationContext(),HomePageActivity.class);
试一试{
对于(Map.Entry:remoteMessage.getData().entrySet())
{
String key=entry.getKey();
字符串值=entry.getValue();
logThis(标记“key”+key+“value”+value);
开关(钥匙){
案例“标题”:
title=entry.getValue();
打破
案例“正文”:
body=entry.getValue();
打破
案例“类型”:
type=entry.getValue().toLowerCase();
打破
}
}
logThis(“jeev”,“Type is:”+Type);
开关(类型){
案例“您的通知类型”:
//在这里编写代码
打破
}
}捕获(例外e){
logThis(标记,“异常:+e.getMessage());
}
}
}

@覆盖
收到消息时公共无效(RemoteMessage RemoteMessage){
if(remoteMessage==null)
返回;
//检查消息是否包含通知负载。
if(remoteMessage.getNotification()!=null){
handleNotification(remoteMessage.getNotification().getBody());
}
//检查消息是否包含数据有效负载。
如果(remoteMessage.getData().size()>0){
字符串标题=”,正文=”,类型=”;
Intent hIntent=新的意图(getApplicationContext(),HomePageActivity.class);
试一试{
对于(Map.Entry:remoteMessage.getData().entrySet())
{
String key=entry.getKey();
字符串值=entry.getValue();
logThis(标记“key”+key+“value”+value);
开关(钥匙){
案例“标题”:
title=entry.getValue();
打破
案例“正文”:
body=entry.getValue();
打破
案例“类型”:
type=entry.getValue().toLowerCase();
打破
}
}
logThis(“jeev”,“Type is:”+Type);
开关(类型){
案例“您的通知类型”:
//在这里编写代码
打破
}
}捕获(例外e){
logThis(标记,“异常:+e.getMessage());
}
}
}

应用程序构建.gradle

implementation 'com.google.firebase:firebase-core:11.6.0'
implementation 'com.google.firebase:firebase-messaging:11.6.0'here
Menifest.xml

 <service android:name=".Notification.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".Notification.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/app_icon" />

MyFirebaseMessagingService.Java

公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{

private static final String TAG = "notifi";
NotificationChannel mChannel;
String CHANNEL_ID="1234";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);




    Intent intent = new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(CHANNEL_ID, "Name", NotificationManager.IMPORTANCE_HIGH);
        mChannel.setLightColor(Color.GRAY);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
        mChannel.setDescription("desc");
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        mChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel( mChannel );
        }
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("MY Notification");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.app_icon);

    //Vibration
    notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
    //LED
    notificationBuilder.setLights(Color.RED, 3000, 3000);

    //Ton
    notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(0,notificationBuilder.build());







}


private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)
            .setContentInfo(notification.getTitle())
            .setLargeIcon(icon)
            .setColor(Color.RED)
            .setLights(Color.RED, 1000, 300)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setSmallIcon(R.mipmap.ic_launcher);


    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Notification Channel is required for Android O and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("channel description");
        channel.setShowBadge(true);
        channel.canShowBadge();
        channel.enableLights(true);

        channel.setLightColor(Color.WHITE);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
        notificationManager.createNotificationChannel(channel);

    }

    notificationManager.notify(0, notificationBuilder.build());

   }


}
private static final String TAG=“notifi”;
通知通道;
字符串通道_ID=“1234”;
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
super.onMessageReceived(remoteMessage);
意向意向=新意向(此,MainActivity.class);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_ONE_SHOT);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
mChannel=新通知频道(频道ID,“名称”,通知管理器。重要性高);
mcchannel.setLightColor(Color.GRAY);
mChannel.enableLights(真);
mChannel.使能振动(真);
设置振动模式(新的长[]{100200300400500});
mcchannel.setDescription(“desc”);
AudioAttributes AudioAttributes=新建AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT\u TYPE\u MUSIC)
.setUsage(AudioAttributes.USAGE_通知)
.build();
mChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,audioAttributes);
如果(通知)
private static final String TAG = "notifi";
NotificationChannel mChannel;
String CHANNEL_ID="1234";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);




    Intent intent = new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(CHANNEL_ID, "Name", NotificationManager.IMPORTANCE_HIGH);
        mChannel.setLightColor(Color.GRAY);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
        mChannel.setDescription("desc");
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        mChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel( mChannel );
        }
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("MY Notification");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.app_icon);

    //Vibration
    notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
    //LED
    notificationBuilder.setLights(Color.RED, 3000, 3000);

    //Ton
    notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(0,notificationBuilder.build());







}


private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)
            .setContentInfo(notification.getTitle())
            .setLargeIcon(icon)
            .setColor(Color.RED)
            .setLights(Color.RED, 1000, 300)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setSmallIcon(R.mipmap.ic_launcher);


    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Notification Channel is required for Android O and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("channel description");
        channel.setShowBadge(true);
        channel.canShowBadge();
        channel.enableLights(true);

        channel.setLightColor(Color.WHITE);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
        notificationManager.createNotificationChannel(channel);

    }

    notificationManager.notify(0, notificationBuilder.build());

   }


}
  <service
        android:name=".FirebaseNotification.FirebaseIDService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".FirebaseNotification.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>
 @Override
 public void onMessageReceived(RemoteMessage remoteMessage) {

  // if (remoteMessage.getData().size() > 0) {
  // sendUserNotification(remoteMessage.getData().get("title"), r . 
  //emoteMessage.getData().get("text"));
  //}

    JSONObject json = null;
    try {
        json = new JSONObject(remoteMessage.getData().toString());
        //Log.e("JSON OBJECT", object.toString());
        JSONObject data = json.getJSONObject("data");
        String titel = data.getString("title");
        String message = data.getString("message");
        String image = data.getString("image");
        sendUserNotification(titel, message,image);
        //rest of the code
    } catch (Exception e) {
        e.printStackTrace();
    }


}


private void sendUserNotification(String title, String mess,String image) {
    Log.e("JSON title", title);
    Log.e("JSON mess", mess);
    int notifyID = 1;
    Intent intent;
    NotificationChannel mChannel;


    NotificationCompat.BigPictureStyle bpStyle = new NotificationCompat.BigPictureStyle();
    //bpStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.fastfood)).build();
    bpStyle.bigPicture(getBitmapfromurl(image));


    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    intent = new Intent(context, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String CHANNEL_ID = context.getPackageName();// The id of the channel.
    CharSequence name = "Sample one";// The user-visible name of the channel.
    int importance = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        importance = NotificationManager.IMPORTANCE_HIGH;
    }
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID);
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
    notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    notificationBuilder.setContentIntent(pendingIntent);
    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mess));
    notificationBuilder.setContentText(mess);
    notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
            R.mipmap.ic_launcher));
    notificationBuilder.setStyle(bpStyle);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationManager.createNotificationChannel(mChannel);
    }
    if (notificationManager != null) {
        notificationManager.notify(notifyID /* ID of notification */, notificationBuilder.build());
    }


}



 public class FirebaseIDService extends FirebaseInstanceIdService {
 private static final String TAG = "FirebaseIDService";
 public static final String FIREBASE_TOKEN = "token";


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    //System.out.println("token=================="+refreshedToken);
    Log.i(TAG, "FCM Registration Token: " + refreshedToken);
    SharedPrefUtil.getInstance(this).put(FIREBASE_TOKEN,refreshedToken);

}

}