gpt4 book ai didi

Android 推送通知服务未在 Lollipop 上启动

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

我在启动推送通知服务时遇到问题。

在 Lollipop 上,我的应用程序在启动通知接收器服务时崩溃。

我的启动服务代码是。

Intent registrationIntent = new Intent(
"com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app",
PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "KEY");
SplashScreen.this.startService(registrationIntent);

Menifest.xml代码

       <receiver
android:name="com.example.notifications.C2DMReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >

<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />

<category android:name="com.app.example" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="com.app.example" />
</intent-filter>
</receiver>

最佳答案

我在我的应用中遇到了同样的问题。

关注这个link

希望你能解决这个问题。

关于您的问题的一些解释。

下载 gcm.jar 并将其添加到您的项目 lib 文件夹中。并在您要注册的 java 类中添加以下代码。

        GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);

// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);

// Check if regid already presents
if (regId.equals("")) {
// Register with GCM
GCMRegistrar.register(this, "SENDERKEY");
} else {
// Device is already registered on GCM Server
if (GCMRegistrar.isRegisteredOnServer(this)) {
String df = "dfd";
} else {
GCMRegistrar.register(this, "SENDERKEY");
}
}

现在您创建一个类 GCMIntentService.java 和下面的代码。

 public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService ";

private static Context mContext;

public GCMIntentService() {
super("SENDERKEY");
}

@Override
protected void onRegistered(Context context, String registrationId) {
Debuger.e(TAG, "Device registered: regId = " + registrationId);
}

@Override
protected void onUnregistered(Context context, String registrationId) {
Debuger.e(TAG, "Device unregistered");
}

@Override
protected void onMessage(Context context, Intent intent) {
Debuger.e(TAG, "Received message");

mContext = context;
String message = intent.getExtras().getString("message");
if (message != null) {
Debuger.e(TAG + "onMessage", "message = " + message);
} else {
message = "";
}
}

@Override
protected void onDeletedMessages(Context context, int total) {
// Debuger.e(TAG, "Received deleted messages notification");
}

@Override
public void onError(Context context, String errorId) {
// Debuger.i(TAG, "Received error: " + errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
// Debuger.e(TAG, "Received recoverable error: " + errorId);
return super.onRecoverableError(context, errorId);
}
}

然后在你的 AndroidMenifest.xml 文件下面添加

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>

<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="com.example.gcm" />
</intent-filter>
</receiver>

<service android:name="com.example.gcm.GCMIntentService" />

关于Android 推送通知服务未在 Lollipop 上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455390/

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