关闭时,Android应用程式未收到FCM通知

关闭时,Android应用程式未收到FCM通知,第1张

概述我正在检查 Firebase Cloud Messaging发送通知.当应用程序处于开放状态时,已经实现了它并收到通知.但如果我关闭应用程序,它不再通知.有没有解决办法. 码: WebRequest wRequest;wRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");wRequest.Method = "po 我正在检查 Firebase Cloud Messaging发送通知.当应用程序处于开放状态时,已经实现了它并收到通知.但如果我关闭应用程序,它不再通知.有没有解决办法.

码:

WebRequest wRequest;wRequest = WebRequest.Create("https://fcm.GoogleAPIs.com/fcm/send");wRequest.Method = "post";wRequest.ContentType = " application/Json;charset=UTF-8";wRequest.headers.Add(string.Format("Authorization: key={0}",AppID));wRequest.headers.Add(string.Format("Sender: ID={0}",SenderID));string postData = "{\"registration_IDs\":[\"" + regIDs + "\"],\"data\": "+ value +"}";Byte[] bytes = EnCoding.UTF8.GetBytes(postData);wRequest.ContentLength = bytes.Length;Stream stream = wRequest.GetRequestStream();stream.Write(bytes,bytes.Length);stream.Close();WebResponse wResponse = wRequest.GetResponse();

消息服务 –

public class MessagingService extends FirebaseMessagingService {    @OverrIDe    public voID onMessageReceived(RemoteMessage remoteMessage) {        Map<String,String>  data = remoteMessage.getData();        sendNotification(data);    }    public voID showMessage(Map<String,String>  serverData) {        Intent i = new Intent(this,MainActivity.class);        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top);        PendingIntent pendingIntent = PendingIntent.getActivity(this,i,PendingIntent.FLAG_UPDATE_CURRENT);        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)                .setautoCancel(true)                .setContentTitle(serverData.get("Title"))                .setContentText(serverData.get("Message"))                .setSmallicon(R.drawable.common_Google_signin_btn_icon_dark)                .setContentIntent(pendingIntent);        notificationmanager manager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);        manager.notify(Integer.parseInt(serverData.get("ItemID")),builder.build());    }    private voID sendNotification(Map<String,String>  serverData) {        Intent intent = new Intent(this,MainActivity.class);        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top);        PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */,intent,PendingIntent.FLAG_UPDATE_CURRENT);        long[] pattern = {500,500,500};        Uri defaultSoundUri= ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)                .setSmallicon(R.drawable.common_Google_signin_btn_icon_dark)                .setContentTitle(serverData.get("Title"))                .setContentText(serverData.get("Message"))                .setautoCancel(true)                .setVibrate(pattern)                .setlights(color.BLUE,1,1)                .setSound(defaultSoundUri)                .setContentIntent(pendingIntent);        notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);        notificationmanager.notify(Integer.parseInt(serverData.get("ItemID")),notificationBuilder.build());    }}

主要活动-

public class MainActivity extends AppCompatActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);       FirebaseMessaging.getInstance().subscribetotopic("test");        FirebaseInstanceID.getInstance().getToken();    }}

表现-

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="test.com.firebasenotify">    <uses-permission androID:name="androID.permission.INTERNET"/>    <uses-permission androID:name="androID.permission.READ_PHONE_STATE" />    <application        androID:allowBackup="true"        androID:icon="@mipmap/ic_launcher"        androID:label="@string/app_name"        androID:supportsRtl="true"        androID:theme="@style/Apptheme">        <activity androID:name=".MainActivity">            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service androID:name=".MessagingService">            <intent-filter>                <action androID:name="com.Google.firebase.MESSAGING_EVENT" />            </intent-filter>        </service>        <service androID:name=".InstanceIDService">            <intent-filter>                <action androID:name="com.Google.firebase.INSTANCE_ID_EVENT" />            </intent-filter>        </service>    </application></manifest>
解决方法 代码没有问题.我正在使用Mi笔记4,不知何故在关闭应用程序时,它不会在Mi4中显示通知.我测试与其他AndroID设备和它的工作正常.

感谢Tim Castelijns和Frank van Puffelen参与了谈话.

总结

以上是内存溢出为你收集整理的关闭时,Android应用程式未收到FCM通知全部内容,希望文章能够帮你解决关闭时,Android应用程式未收到FCM通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存