我正在尝试将推送通知添加到我的Android应用中。 一切似乎都没问题:
...但是通知永远不会显示,但是:
这是我的代码。关于我做错了什么的任何线索?谢谢!
清单(部分)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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="<MY-APP>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="<MY-APP>.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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="<MY-APP>" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
广播接收器
package <MY-APP>;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("gcm_debug", "PushReceiver onReceive called");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String msgType = gcm.getMessageType(intent);
if(!extras.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(msgType)){
Log.i("gcm_debug", "Message send error");
}else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(msgType)){
Log.i("gcm_debug", "Message deleted");
}else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(msgType)){
Log.i("gcm_debug", "Message received : " + extras.toString());
}
}
setResultCode(Activity.RESULT_OK);
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
在您的Google Play控制台上,您需要确保要发送通知的服务器IP为确定。
您可以为本地开发服务器提供0.0.0.0/0
ip,为生产服务器提供真实IP。
就像这样。