我按照http://developer.android.com/guide/google/gcm/gs.html上给出的步骤操作,每次尝试从我的设备注册时都会收到以下错误(在froyo上运行)
07-04 22:48:31.178:D/C2DMRegistrar(3880):[C2DMRegistrar.66]注册:http错误400
对于senderId,我尝试过使用projectID(根据Google),电子邮件ID和API密钥但没有效果.
我有Android市场并在设备上签署了谷歌帐户.
搜索有关如何使用GCM但使用PHP而不是servlet发送通知的一些信息,我发现:GCM与PHP(Google云消息传递)
我测试了这些问题的响应的工作代码,我也创建了一个用于浏览器应用程序的密钥(带有引用者),并且我给了这个ip的权限:.mywebsite.com /(te php文件在这个网址上:" http: //www.mywebsite.com/~jma/cHtml5/cap/kk.php ")
但我得到了这样的回应:未经授权的错误401
我做错了什么?
这是php文件:
<?php
// Replace with real server API key from Google APIs
$apiKey = "fictional key";
// Replace with real client registration IDs
$registrationIDs = array( "APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ");
// Message to be sent
$message = "Test Notificación PHP";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json' …Run Code Online (Sandbox Code Playgroud) php android push-notification android-c2dm google-cloud-messaging
我试图在我的应用程序上使用GCM,但GCMRegistrar.register总是返回空字符串.这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="net.andromedya.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="net.andromeya.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="net.andromedya.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<permission
android:name="net.andromedya.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<activity
android:name="net.andromedya.activities.MainActivity" …Run Code Online (Sandbox Code Playgroud) 我正在使用推送通知开发Android应用程序.
我想在调用onResume方法时清除推送通知中出现的状态栏中的小图标.
然后,我编写以下代码,但它不能正常工作.
※※PushHandlerActivity.java
@Override
public void onResume() {
super.onResume();
GCMIntentService.cancelNotification(this);
finish();
}
Run Code Online (Sandbox Code Playgroud)
※※GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
public static final int NOTIFICATION_ID = 1234;
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super("GCMIntentService");
}
public void createNotification(Context context, Bundle extras)
{
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(this);
Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("pushBundle", extras);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(context.getApplicationInfo().icon) …Run Code Online (Sandbox Code Playgroud) android push-notification android-notifications google-cloud-messaging
我正在开发一个Android项目,我需要使用GCM来推送消息.我想只使用PHP而不是HTML发送通知.我该如何实现这一目标?我尝试的方式只是将gcm_regid和消息作为参数放到$ gcm-> send_notification($ registration_ids,$ msg)但我不断收到错误说: - 字段"data"必须是JSON数组:["你有一个通知!"]
在哪里"你有通知!" 是我的消息!! 请帮忙!!谢谢!
send_notification的代码是
public function send_notification($registatoin_ids, $message) {
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, …Run Code Online (Sandbox Code Playgroud) 在我的常规构建gcm工作正常,但当我使用flavor改变com名称时,事情就停止了.我的IntentService永远不会被初始化.我应该如何设置我的清单文件以便发生这种情况?
我得到了日志
V/GCMBroadcastReceiver? onReceive: com.google.android.c2dm.intent.REGISTRATION
V/GCMBroadcastReceiver? GCM IntentService class: com.sample.app.dev.GCMIntentService
V/GCMBaseIntentService? Acquiring wakelock
Run Code Online (Sandbox Code Playgroud)
然后什么也没有.我的自定义IntentService类永远不会被初始化,我的registrationId保持为空.我目前的实现类似于这里的实现:https: //stackoverflow.com/a/20334397/198034
我应该如何设置我的清单以获得0.11.+味道与gcm一起使用?
下面是我的一个版本的清单,我在主清单中有更多代码(所有需要的权限等),但没有其他处理gcm.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.app"
android:versionCode="45"
android:versionName="0.6.5" >
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.sample.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sample.app.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name" >
<service android:name=".GCMIntentService" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${packageName}" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud) android gradle push-notification google-cloud-messaging android-gradle-plugin
我正在运行这些命令以获取令牌:
InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
String token = instanceID.getToken(Config.GOOGLE_PROJECT_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Run Code Online (Sandbox Code Playgroud)
但得到这个例外:
java.io.IOException: MISSING_INSTANCEID_SERVICE
at com.google.android.gms.iid.zzc.zza(Unknown Source)
at com.google.android.gms.iid.zzc.zzb(Unknown Source)
at com.google.android.gms.iid.zzc.zza(Unknown Source)
at com.google.android.gms.iid.InstanceID.zzc(Unknown Source)
at com.google.android.gms.iid.InstanceID.getToken(Unknown Source)
at autofocus.com.autofocus.activity.ItemListActivity$4.doInBackground(ItemListActivity.java:408)
at autofocus.com.autofocus.activity.ItemListActivity$4.doInBackground(ItemListActivity.java:398)
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
我按照/sf/answers/2703847891/中的说明将SNS与FCM连接到Android应用。部署到模拟器后,该应用程序会初始化,但不会收到任何消息。同一款应用在我的实际Nexus 6设备上可以正常运行并收到消息。为什么会有这种差异?
android amazon-web-services amazon-sns firebase google-cloud-messaging
我的应用正在正确接收通知,但未能显示收到信息的通知(如果应用已打开).
注意:如果应用程序在后台,则会显示通知而不会出现任何问题.
我的代码:
我收到此方法的通知:
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.d(TAG, "From: " + remoteMessage.getFrom());
if(remoteMessage!=null)
{
String id = null;
String title = null;
String body = null;
String launchPage = null;
if(remoteMessage.getNotification()!=null)
{
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
}
if(remoteMessage.getMessageId()!=null)
{
id = remoteMessage.getMessageId();
}
Log.i(TAG, "id: " + id);
Log.i(TAG, "title: "+ title);
Log.i(TAG, "body: " + body);
int notif_id = 0;
sendNotification(notif_id, title, body, launchPage);
}
}
Run Code Online (Sandbox Code Playgroud)
然后它调用这个(它应该显示我理解的通知):
private void sendNotification(int id, String title, String …Run Code Online (Sandbox Code Playgroud) android ×10
android-c2dm ×3
firebase ×2
php ×2
amazon-sns ×1
battery ×1
gradle ×1
html ×1
json ×1