Tôi đang cố triển khai thông báo đẩy bằng GCM trong Ứng dụng Android của mình. Tôi đã tạo một máy chủ để đẩy tin nhắn đến các thiết bị thành công và cũng tạo ra một api để lưu trữ mã thông báo thiết bị trong máy chủ thành công. Đã tạo ứng dụng khách trong ứng dụng được đề cập trong số link dựa trên số sample project. Và cũng tạo ra một chức năng trong php để đẩy thông báo cho ứng dụng. Khi tôi chạy hàm đó trong máy chủ, tôi nhận được phản hồi thành công. Điều đó có nghĩa là tin nhắn được gửi từ gcm đến di động. Nhưng thông báo không được hiển thị thay vào đó tôi nhận được thông tin sau trong con mèo đăng nhập.Sự cố khi nhận thông báo đẩy trên Ứng dụng khách GCM
06-05 14:58:59.172 23693-28001/com.greenboards.base I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setColor, referenced from method com.google.android.gms.gcm.zza.zzv
06-05 14:58:59.172 23693-28001/com.greenboards.base W/dalvikvm﹕ VFY: unable to resolve virtual method 184: Landroid/app/Notification$Builder;.setColor (I)Landroid/app/Notification$Builder;
06-05 14:58:59.173 23693-28001/com.greenboards.base D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0068
06-05 14:58:59.175 23693-28001/com.greenboards.base W/GcmNotification﹕ Failed to show notification: Missing icon
Để chụp thư tôi đang sử dụng cùng dịch vụ người nghe được biểu thị trong ứng dụng mẫu (tôi đã thêm bên dưới để tham khảo). Vì vậy, tôi nghĩ rằng vấn đề trong trình quản lý thông báo. Vì vậy, tôi đã nhận xét và chạy ứng dụng. Nhưng một lần nữa tôi nhận được phản hồi tương tự mà không cần bất kỳ thông báo nào. Tôi không biết vấn đề là gì.
package com.greenboards.base;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.greenboards.base.R;
import com.google.android.gms.gcm.GcmListenerService;
import com.greenboards.base.SplashScreen;
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
}
// [END receive_message]
/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
/*Intent intent = new Intent(this, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 *//* ID of notification *//*, notificationBuilder.build());*/
Log.v("notification message",message);
}
}
Nhưng bất cứ khi nào thư nhận được từ máy chủ onMessageReceived
phải được gọi trong trình lắng nghe ở trên. Trong chức năng onMessageReceived
có hai chức năng ghi nhật ký để hiển thị tin nhắn và người gửi. Điều đó cũng không thực thi. Điều đó có nghĩa là chính chức năng đó không thực thi. Bên dưới, tôi đã thêm nội dung tệp kê khai.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.greenboards.base"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<!-- Application Permissions -->
<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.greenboards.base.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.greenboards.base.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Application Configuration and Activities -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.greenboards.base" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.greenboards.base.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.greenboards.base.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name="com.greenboards.base.RegistrationIntentService"
android:exported="false">
</service>
</application>
</manifest>
Hiện tại tôi không thể gỡ lỗi vấn đề là gì. Tôi có làm gì sai hay tôi đang thiếu gì?
Tôi vừa tình cờ gặp vấn đề tương tự. Tuy nhiên, tôi không nhận được cảnh báo '' 'W/GcmNotification ﹕ Không hiển thị thông báo: Thiếu biểu tượng'''. Bạn cũng có thể hiển thị những gì bạn đang gửi đến gcm (yêu cầu curl của bạn) – peshkira
Bạn có đang thử nghiệm trên thiết bị có API Android 21 không? setColor đã được thêm vào trong 21 và có vẻ như một số thư viện vẫn sử dụng Notification, chứ không phải NotificationCompat. – Koh
Thử triển khai các thay đổi được đề xuất bởi @rohitsakala và cập nhật kho lưu trữ hỗ trợ, thư viện và dịch vụ phát của bạn từ trình quản lý SDK. Hi vọng điêu nay co ich. – Koh