未在Android中设置Firebase云消息传递背景通知渠道ID O

问题描述:

Firebase似乎没有在传入消息中设置通知渠道ID给Android M.我正在关注this guide,并试图在我的应用程序获取通知时触发在后台。未在Android中设置Firebase云消息传递背景通知渠道ID O

这是我的应用程序代码和清单。

public class MainActivity extends AppCompatActivity { 

    private void registerNotificationChannel(String id, String name, String description) { 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     int importance = NotificationManager.IMPORTANCE_HIGH; 
     NotificationChannel mChannel = new NotificationChannel(id, name, importance); 
     mChannel.setDescription(description); 
     mChannel.enableLights(true); 
     mChannel.setLightColor(Color.RED); 
     mChannel.enableVibration(true); 
     mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
     mNotificationManager.createNotificationChannel(mChannel); 
    } 

    //private BroadcastReceiver isms; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG,"MainActivity.oncreate. token is:" + FirebaseInstanceId.getInstance().getToken()); 

     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     registerNotificationChannel("default","default","all other notifications"); 
     registerNotificationChannel("channel1","channel1","notification channel 1"); 
     registerNotificationChannel("channel2","channel2","notification channel 2"); 
    } 

    @Override protected void onDestroy() { 
     super.onDestroy(); 
    } 

} 

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.d(TAG, "firebase message received"); 
     if (remoteMessage.getNotification() != null) { 
      Map<String,String> data = remoteMessage.getData(); 
      for (Map.Entry<String, String> entry : data.entrySet()) 
      { 
       Log.d(TAG, "data: " + entry.getKey() + "/" + entry.getValue()); 
      } 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 

    } 
} 

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 
    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG, "Refreshed firebase " + 
       "" + 
       "" + 
       "token: " + refreshedToken); 
    } 
} 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="org.phauna.alerter"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <meta-data 
      android:name="com.google.firebase.messaging.default_notification_channel_id" 
      android:value="default"/> 

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

    </application> 

</manifest> 

正如你所看到的,我肯定我的注册通知渠道,因为它说here。这从我的设备屏幕截图确认信道被注册,以及:

enter image description here

这里的关键部分。 当我定位API版本25时,我收到后台通知。只要我瞄准版本26,我就不会。相反, 我在logcat中收到此错误信息:

10-11 12:40:00.925 899 8910 E NotificationService: No Channel found for pkg=org.phauna.alerter, channelId=null, id=0, tag=GCM-Notification:286245598, opPkg=org.phauna.alerter, callingUid=10179, userId=0, incomingUserId=0, notificationUid=10179, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE) 

我肯定设置频道ID,在这个截图中描绘从火力地堡控制台发送:

enter image description here

我有也尝试通过HTTP服务发送:

curl -X POST --header "Authorization: key=<redacted>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<redacted>\",\"data\":{\"android_channel_id\":\"channel1\"},\"notification\":{\"body\":\"this is a testfoobar\"}}" 

在这两种情况下,消息都会到达我的设备b ut没有channelId(如上述日志消息中的channelId = null所示)。

在前台,我可以用服务捕获通知并手动粘贴一个通道ID(即使我必须在消息的有效负载中对​​通道ID进行编码)。但是我需要后台通知才能正常工作,并且据我所知,Firebase库的正确性取决于此。

+1

你正在搭建什么版本的'firebase-messaging'?在使用11.4.2构建的我的测试应用程序中,当收到未注册频道ID的通知时,我会看到通知和此日志消息:“W/FirebaseMessaging:Notification Channel requested(channel_X)尚未由应用程序创建。将使用清单配置或默认值。' –

+0

嗯,这可能是问题。我只是使用firebase助手来安装firebase-messaging包,它会自动选择10.0.1。让我更新并重试。 –

Firebase Release Notes的表明,在10.2.6版溶液中加入对通知信道支持(2017年5月17日):

为Androidö通知信道增加的支持。 Android客户端 可在应用程序清单 中指定默认通知通道,如果下游消息不包含 notification_channel参数,则将使用该通知通道。

更新您的版本以使用版本10.2.6或更高版本。

+0

我更新到11.4.2,我的问题已修复!感谢Bob! –

+0

我无法找到* android_channel_id ** @Bob你知道它在哪里。 –

+0

@MaheshGawhane:不确定你的意思。可能是默认频道[此处介绍](https://firebase.google.com/docs/cloud-messaging/android/client#manifest)。 –