gpt4 book ai didi

android - 应用程序关闭或最小化时推送通知没有声音

转载 作者:行者123 更新时间:2023-11-29 17:01:22 24 4
gpt4 key购买 nike

我正在使用 fcm 在我的 Android 应用程序中获取推送通知。当应用程序打开和关闭或最小化状态时,我也能很好地收到通知。但问题是当应用程序处于最小化或关闭状态时,我没有收到通知声音。

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
String refrehedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("refreshedtoken",refrehedToken+"");
PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.DEVICETOKEN,refrehedToken);
sendRegistrationToServer(refrehedToken);

}

private void sendRegistrationToServer(String refrehedToken) {

}

}

////////////////////////////////////////////////////////////////

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

if(remoteMessage.getData().size()>0){
Log.d(TAG,"From:"+remoteMessage.getData()+"");
Gson gson = new Gson();
String json = gson.toJson(remoteMessage.getData());
Log.d("jsonresponce",json+",,,");
try {
JSONObject object = new JSONObject(json);
String title = object.getString("title");
String message = object.getString("message");
Log.d("firebasenotification",title+",,"+message);
sendNotificatin(title,message);
} catch (JSONException e) {
e.printStackTrace();
}
}else if(remoteMessage.getNotification()!=null){
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle());
sendNotificatin(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

}
}

private void sendNotificatin(String title, String message) {
Intent intent = new Intent(this,SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultsoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_ic)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int num = random.nextInt(99999-1000)+1000;
notificationManager.notify(num,notificationBuilder.build());

}
}

/////////////////////////////////////////////////////

   <service android:name=".Fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Fcm.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="@mipmap/app_ic" />

最佳答案

我没有足够的声誉来添加评论,所以我这样做是为了你,但我知道这是不合适的。

原因是当app最小化,或者在后台时,通知投递到Android notification delivered to Android notification tray。有两种有效载荷,一种是“数据有效载荷”,另一种是“通知有效载荷”。只需删除“通知负载”即可正常工作。

关于android - 应用程序关闭或最小化时推送通知没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com