2019年7月开始工作
Android编译SdkVersion 28、buildToolsVersion 28.0.3和firebase-message:19.0.1
在对所有其他StackOverflow问题和答案进行了数小时的研究,并尝试了无数过时的解决方案后,此解决方案在以下三种情况下显示了通知:
-应用程序位于前台:
通知是由我的MyFirebaseMessagingService类中的onMessageReceived方法接收的
-应用程序已被终止(它未在后台运行):FCM会自动将通知发送到通知托盘。当用户触摸通知时,通过调用具有android.intent.category的活动来启动应用程序。舱单上的发射器。您可以在onCreate()方法中使用getIntent().getExtras()来获取通知的数据部分。
-应用程序处于后台:FCM会自动将通知发送到通知托盘。当用户触摸通知时,通过启动包含android.intent.category的活动,应用程序将显示在前台。舱单上的发射器。由于我的应用程序在该活动中具有launchMode=“singleTop”,因此不会调用onCreate()方法,因为已经创建了同一类的一个活动,而是调用该类的onNewIntent()方法并使用intent.getExtras()获取通知的数据部分。
步骤:1-如果您这样定义应用程序的主要活动:
<活动android:name=“.MainActivity”android:label=“@string/app_name”android:largeHeap=“true”android:screenOrientation=“纵向”android:launchMode=“singleTop”><意向过滤器><action android:name=“.MainActivity”/><action android:name=“android.intent.action.MAIN”/><category android:name=“android.intentation.category.LAUNCHER”/><category android:name=“android.intentation.category.DEFAULT”/></into-filter></活动>
2-在MainActivity.class的onCreate()方法中添加这些行
意向i=getIntent();捆绑额外项=i.getExtras();if(额外!=空){for(字符串键:extras.keySet()){对象值=extras.get(key);Log.d(Application.APPTAG,“onCreate:Key:”+Key+“Value:”+Value处收到的额外服务);}String title=extras.getString(“title”);字符串消息=extras.getString(“body”);if(message!=空&&message.length()>0){getIntent().removeExtra(“主体”);showNotificationInADialog(标题、消息);}}
将这些方法添加到同一MainActivity.class:
@覆盖NewIntent上的公共无效(意向-意图){//创建此类的新意图时调用。//主要的情况是当应用程序处于后台时,通知到达托盘,用户触摸通知super.onNewIntent(意向);Log.d(Application.APPTAG,“onNewIntent-启动”);捆绑额外功能=intent.getExtras();if(额外!=空){for(字符串键:extras.keySet()){对象值=extras.get(key);Log.d(Application.APPTAG,“在onNewIntent:Key:”+Key+“Value:”+Value上接收的附加值);}String title=extras.getString(“title”);字符串消息=extras.getString(“body”);if(message!=空&&message.length()>0){getIntent().removeExtra(“主体”);showNotificationInADialog(标题、消息);}}}private void showNotificationInADialog(字符串标题,字符串消息){//显示带有所提供标题和消息的对话框警报对话框。Builder Builder=新AlertDialog。建筑商(本);builder.setTitle(标题);builder.setMessage(消息);builder.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){public void onClick(DialogInterface对话框,int whichButton){dialog.cancel();}});AlertDialog警报=builder.create();alert.show();}
3-创建类MyFirebase,如下所示:
软件包com.yourcompany.app;导入android.content。意图;导入android.util。日志;导入com.google.firebase.messaging。FirebaseMessagingService;导入com.google.firebase.messaging。远程消息;公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{公共MyFirebaseMessagingService(){super();}@覆盖public void onMessageReceived(远程消息远程消息){Log.d(Application.APPTAG,“myFirebaseMessagingService-onMessageReceived-message:”+remoteMessage);意向对话框Intent=新意向(this,NotificationActivity.class);dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);dialogIntent.putExtra(“消息”,远程消息);startActivity(dialogIntent);}}
4-创建一个新类NotificationActivity.class,如下所示:
软件包com.yourcompany.app;导入android.app。活动;导入android.app。警报对话框;导入android.content。DialogInterface;导入android.os。捆绑;导入android.util。日志;导入androidx.appcompat.app。应用兼容活动;导入androidx.appcompat.view。上下文主题包装器;导入com.google.firebase.messaging。远程消息;公共类NotificationActivity扩展了AppCompatActivity{私人活动情境;@覆盖protected void onCreate(捆绑保存的实例状态){super.onCreate(savedInstanceState);上下文=此;捆绑额外项=getIntent().getExtras();Log.d(Application.APPTAG,“NotificationActivity-onCreate-extras:”+额外项);if(额外==空){context.finish();回报;}RemoteMessage msg=(远程消息)extras.get(“msg”);if(消息==空){context.finish();回报;}远程消息。通知通知=msg.getNotification();if(通知==空){context.finish();回报;}字符串对话框消息;尝试{dialogMessage=通知.getBody();}catch(异常e){context.finish();回报;}String dialogTitle=通知.getTitle();if(dialogTitle==null||dialogTile.length()==0){dialogTitle=“”;}警报对话框。Builder Builder=新AlertDialog。生成器(新ContextThemeWrapper(context,R.style.myDialog));builder.setTitle(dialogTitle);builder.setMessage(dialogMessage);builder.setPositiveButton(getResources().getString(R.string.accept),新的DialogInterface。OnClickListener(){public void onClick(DialogInterface对话框,int whichButton){dialog.cancel();}});AlertDialog alert=builder.create();alert.show();}
}
5-将这些行添加到标签内的应用程序清单中
<服务android:name=“.MyFirebaseMessagingService”android:exported=“false”><意向过滤器><action android:name=“com.google.firebase.MESSAGING_EVENT”/></into-filter></服务><meta-data安卓:name=“com.google.firebase.message.default_notification_channel_id”安卓:value=“@string/default_noteification_cchannel_id”/><activity android:name=“.NotificationActivity”android:theme=“@style/myDialog”></activity><元数据android:name=“com.google.firebase.message.default_notification_icon”android:resource=“@drawable/notification_icon”/><元数据android:name=“com.google.firebase.message.default_notification_color”android:resource=“@color/color_accent”/>
6-在Application.java onCreate()方法或MainActivity.class onCreate()方法中添加以下行:
//通知频道创建if(Build.VERSION.SDK_INT>=Build.VERTION_CODES.O){//创建频道以显示通知。String channelId=getResources().getString(“default_channel_id”);字符串channelName=getResources().getString(“常规公告”);NotificationManager NotificationManager=getSystemService(NotificationManager.class);notificationManager.createNotificationChannel(新的通知频道(channelId,channelName、NotificationManager。IMPORTANCE_LOW));}
完成。
现在,为了在上述3种情况下都能正常工作,您必须通过以下方式从Firebase web控制台发送通知:
在“通知”部分中:通知标题=通知对话框中显示的标题(可选)通知文本=要向用户显示的消息(必需)然后在Target部分:App=你的Android应用程序和“其他选项”部分中:Android通知频道=default_Channel_id自定义数据key:title值:(此处的文本与Notification部分的title字段中的文本相同)key:body值:(此处的文本与Notification部分的Message字段中的文本相同)键:click_action value:。主要活动声音=禁用
到期时间=4周
您可以使用API 28和Google Play在Emulator中调试它。
快乐编码!