Php 安卓谷歌云消息-推送发送到设备,但设备未显示推送通知
我有一些服务器端代码,我正试图用这些代码创建推送通知。结果如下所示:Php 安卓谷歌云消息-推送发送到设备,但设备未显示推送通知,php,android,curl,google-cloud-messaging,Php,Android,Curl,Google Cloud Messaging,我有一些服务器端代码,我正试图用这些代码创建推送通知。结果如下所示: {"multicast_id":8714083978034301091,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1350807053347963%9aab4bd8f9fd7ecd"}]} @Override protected void onMessage(Context ctxt, Intent message) {
{"multicast_id":8714083978034301091,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1350807053347963%9aab4bd8f9fd7ecd"}]}
@Override
protected void onMessage(Context ctxt, Intent message) {
Bundle extras=message.getExtras();
for (String key : extras.keySet()) {
Log.d(getClass().getSimpleName(),
String.format("onMessage: %s=%s", key,
extras.getString(key)));
}
generateNotification(ctxt, extras.getString("message"), "test title");
这是我用来发送推送通知的PHP代码。顺便说一句,推动没有发生,所以我正在试图找出原因:
$url = 'https://android.googleapis.com/gcm/send';
$device_ids = array( $device_id );
$t_data = array();
$t_data['message'] = 'Someone commented on your business.';
$t_json = array( 'registration_ids' => $device_ids , 'data' => $t_data );
// Open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: key=my_key', 'Content-Type: application/json' ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $t_json ) );
curl_setopt($ch, CURLOPT_URL, $url);
// Execute post
$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
以下是清单中处理Google云消息的部分:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.problemio"
android:versionCode="82"
android:versionName="2.2.82" >
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>
<!-- <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="xx"/> -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Required permission to use in-app billing. -->
<uses-permission android:name="com.android.vending.BILLING" />
<permission android:name="com.problemio.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.problemio.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomTheme"
android:name="MyApplication"
android:debuggable="true"
>
<!-- For Google Cloud Messaging -->
<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="com.problemio" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<!-- End of Google Cloud Messaging -->
}
但无论我尝试什么,推送通知都不会出现在物理设备上。你知道为什么不能,以及我如何让它出现在设备上吗?你需要在
onMessage()
方法中手动生成一个通知。onMessage()只是一个方法,当你的应用程序收到来自服务器的提示时,它将被调用。您可能并不总是希望向用户生成可见的状态栏通知。因此,谷歌提供了灵活性,只在你愿意的情况下发布通知。无论如何,下面的代码将帮助您生成此通知。很明显,你可以根据自己的需要调整它
private void generateNotification(Context context, String message)
{
// put an icon for your notification in your res/drawable folder
// and then get the icon as an int
int icon = R.drawable.notif_icon_name;
long when = System.currentTimeMillis(); // can change this to a future time if desired
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, BaseActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(0, notification);
}
只需向onMessage()添加对通知函数的调用,如下所示:
{"multicast_id":8714083978034301091,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1350807053347963%9aab4bd8f9fd7ecd"}]}
@Override
protected void onMessage(Context ctxt, Intent message) {
Bundle extras=message.getExtras();
for (String key : extras.keySet()) {
Log.d(getClass().getSimpleName(),
String.format("onMessage: %s=%s", key,
extras.getString(key)));
}
generateNotification(ctxt, extras.getString("message"), "test title");
}这里的成功意味着服务器成功地将云消息发布到google服务器。我认为服务器没有办法知道推送消息是否通过。@KKD谢谢-奇怪的是,如果curl成功,那么我无法理解为什么手机没有收到推送通知:)我可以想到两种可能的情况……(1)手机没有连接到互联网(我相信你已经检查过了)……(2)你连接的wifi是防火墙的……@KKD很有趣,但两者都不是:)…我在用它伤脑筋:)请做。。。。我还想知道答案……:)谢谢你,我有你建议的类似的东西。我把我的代码添加到我的原始问题中。在我的代码中是否有一些我做得不对的地方?谢谢你-另一个答案帮助了我,但我会检查你的一些其他问题/答案,并向你投票:)他说的有什么不同吗?就是这样-谢谢!我只被允许在17小时内颁发赏金。那我就给你:)