Android 如何显示来自推送通知fcm的警报
我在firebase例程中有下面的代码。收到通知时,如何将通知消息或数据显示为警报?我服务器上的php代码通过id成功地将消息推送到android设备,收到消息时会听到声音,但我希望显示一个警报或传输到片段(我更喜欢警报)Android 如何显示来自推送通知fcm的警报,android,push-notification,Android,Push Notification,我在firebase例程中有下面的代码。收到通知时,如何将通知消息或数据显示为警报?我服务器上的php代码通过id成功地将消息推送到android设备,收到消息时会听到声音,但我希望显示一个警报或传输到片段(我更喜欢警报) 公共类MyFirebaseMessagingService扩展了FirebaseMessagingService { 私有静态最终字符串TAG=“MyFirebaseMessService”; @凌驾 收到消息时公共无效(RemoteMessage RemoteMessage
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService
{
私有静态最终字符串TAG=“MyFirebaseMessService”;
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage)
{
commonfunc.myprint(“+remoteMessage.getFrom()”);
如果(remoteMessage.getData().size()>0)
{
commonfunc.myprint(“MyFirebaseMessService_getdata 2:+remoteMessage.getdata());
Map data=remoteMessage.getData();
字符串值1=data.get(“dtitle”);
字符串值2=data.get(“dbody”);
commonfunc.myprint(“MyFirebaseMessService_getdata 2:+value1+”+value2);
sendNotification(remoteMessage.getData().toString());
}
if(remoteMessage.getNotification()!=null)
{
commonfunc.myprint(“MyFirebaseMessService_getNot 3正文:”+remoteMessage.getNotification().getBody());
commonfunc.myprint(“MyFirebaseMessService_getNot 3标题:”+remoteMessage.getNotification().getTitle());
sendNotification(remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getTitle());
/*
最后一个字符串mMessage=remoteMessage.getNotification().getBody();
Handler h=新处理程序(Looper.getMainLooper());
h、 post(新的Runnable(){
公开募捐
{
AlertDialog AlertDialog=新建AlertDialog.Builder(MyFirebaseMessagingService.this).create();
alertDialog.setTitle(“消息”);
setMessage(“消息如下:+M消息”);
alertDialog.setButton(alertDialog.BUTTON_中性,“确定”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
alertDialog.show();
返回;
}
});
*/
}
}
私有void sendNotification(字符串体)
{
commonfunc.myprint(“MyFirebaseMessService_sendNotification 4a声音”);
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);
Uri notificationSound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notifiBuilder=新建NotificationCompat.Builder(此)
.setAutoCancel(真)
.setContentTitle(“Firebase云消息传递”)
.setContentText(正文)
.setSound(通知声音)
.setSmallIcon(life.poa.webcastman.poa1.R.drawable.common\u google\u sign\u btn\u icon\u dark)
.setContentIntent(挂起内容);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
notificationManager.notify(0,notifiBuilder.build());
commonfunc.myprint(“MyFirebaseMessService_sendNotification 4b sound”);
//Toast.makeText(getApplicationContext(),“\uuuuuuuuuMyFireBaseMessagingService”+正文,Toast.LENGTH\uShort.show();
}
}
使用本地广播:将此内容放在MessageReceived中:
Intent intent = new Intent("myFunction");
// add data
intent.putExtra("value1", value1);
intent.putExtra("value2", value2);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
在您的活动/片段中:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String t = intent.getStringExtra("value1");
String t1 = intent.getStringExtra("value2");
//alert data here
}
};
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mMessageReceiver,
new IntentFilter("myFunction"));
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(mMessageReceiver);
}
您是否在MessageReceived中获取了数据?是的,下面是我的php,我在firebase例程中获取了通知和数据,并将它们写入日志$json_array=[“notification”=>[“title”=>“nTEST”,“sound”=>“default”,“body”=>“nollardata”],“data”=>[“dtitle”=>“dTEST”,“sound”=>“default”,“dbody”=>“dollardata”],“至”=>$target,“优先级”=>“高”]$body=json_encode($json_数组);您的通知是否显示了数据?另外,我从1:XXXXXXXXXXXX I/System进入我的android logV/FA:处理排队服务任务:1 I/System.out:myprint:{dtitle=dTEST,dbody=dollardata,sound=default}I/System.out:myprint:MyFirebaseMessService_getdata 2:dTEST dollardata etcI在我的android studio日志和顶部的模拟中显示它,但我想这也可以显示为警报对话框吗?所以基本上将例程1放在on messagereceived函数中。。。firebase片段和my函数底部的私有广播是一个新片段..正确吗?@BillFoyer Nope..只有onMessageReceived中的第一部分代码和片段中的私有广播
myFunction
只是一个名称!你可以说出任何名字!这不是你的名字,谢谢,回答得很好。我避免了onPause部分,让通知显示在所有的应用程序屏幕上,所以我必须在每个活动或片段中写入它
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String t = intent.getStringExtra("value1");
String t1 = intent.getStringExtra("value2");
//alert data here
}
};
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mMessageReceiver,
new IntentFilter("myFunction"));
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(mMessageReceiver);
}