gpt4 book ai didi

java - 无法使用 GCM 向 android 发送推送

转载 作者:行者123 更新时间:2023-11-29 20:08:00 24 4
gpt4 key购买 nike

对不起我的英语。我已经花了 2 天,但我无法向 android 发送推送消息。我使用谷歌云消息。例如在 gcm 中我创建了一个新项目并且我有一个 id:

enter image description here

然后我启用了 gcm 并添加了服务器 key

enter image description here

我有这样的代码:

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alexy.gcmclient">

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<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" />

<permission android:name="com.hmkcode.android.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.hmkcode.android.gcm.permission.C2D_MESSAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.hmkcode.android.gcm" />
</intent-filter>
</receiver>

<service
android:name=".GcmMessageHandler"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>


</manifest>

GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

Log.e("GcmBroadcastReceiver", "GcmBroadcastReceiver");
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());

// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}

GcmMessageHandler

public class GcmMessageHandler extends IntentService {

String mes;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
Log.e("GcmMessageHandler", "GcmMessageHandler");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);

Log.e("message", messageType);
}

主要

public class MainActivity extends Activity implements OnClickListener{

Button btnRegId, unregister;
EditText etRegId;
GoogleCloudMessaging gcm;
String regid;
String PROJECT_NUMBER = "308****";

private BroadcastReceiver mRegistrationBroadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

unregister = (Button) findViewById(R.id.unregister);
btnRegId = (Button) findViewById(R.id.btnGetRegId);
etRegId = (EditText) findViewById(R.id.etRegId);

btnRegId.setOnClickListener(this);


}

public void getRegId(){
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";

try {

InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
String token = instanceID.getToken(PROJECT_NUMBER,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

msg = token;
//apiKey = msg;
GcmPubSub.getInstance(getApplicationContext()).subscribe(token, "/topics/users", null);

} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}



return msg;
}

@Override
protected void onPostExecute(String msg) {
etRegId.setText(msg + "\n");
Log.e("key", msg);

}
}.execute(null, null, null);
}

@Override
public void onClick(View v) {
getRegId();
}

}

我使用 service并尝试向设备发送一些推送消息。在服务中它说成功。但是android中的推送没有来(我在日志中没有输出)

最佳答案

我假设你的应用程序包名称必须是 com.example.alexy.gcmclient 或替换包名称 build.gradle

defaultConfig 
applicationId "com.yourpackge"

<permission
android:name="com.example.alexy.gcmclient.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<uses-permission
android:name="com.example.alexy.gcmclient.permission.C2D_MESSAGE" />



<receiver android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.alexy.gcmclient" />
</intent-filter>
</receiver>

关于java - 无法使用 GCM 向 android 发送推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434143/

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