Google云消息 - 通知USB行为

时间:2015-01-29 19:08:55

标签: android push-notification google-cloud-messaging android-notifications

对不起,我的英语不太好......无论如何。

我有一个使用GCM接收推送通知的Android应用程序。

步骤: 1 - 用户单击登录按钮 2 - Android应用程序保存注册ID(工作) 3 - 发送到WebService并放入数据库(工作) 4 - 服务器应用程序(C#)发送所选注册ID(工作)的消息

5 - 设备正常接收通知,但是设备是否已连接 在USB上( *问题*

如果我没有在USB上连接设备,我收到通知但是有一个 空白信息。

我被封了一个星期..有人可以帮帮我吗? PLZ ..

public class GCMNotificationIntentService扩展了IntentService {

public static  int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GCMNotificationIntentService() {
    super("GcmIntentService");
}

public static final String TAG = "GCMNotificationIntentService";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i < 3; i++) {
                Log.i(TAG,
                        "Working... " + (i + 1) + "/5 @ "
                                + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }

            }
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

            sendNotification(""+ extras.get(Config.MESSAGE_KEY));
            Log.i(TAG, "Received: " + extras.toString());
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    Log.d(TAG, "Preparing to send notification...: " + msg);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    long[] pattern = {500,500,500,500,500,500,500,500,500};

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Uri alarmSound2 = Uri.parse("android.resource://"  + getPackageName() + "/" + R.raw.mario);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, lucasbergamo.minha_granna.Android.ac_Login.class), 0);

    String date = DateFormat.getDateTimeInstance().format(new Date());

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this)

            .setSmallIcon(R.drawable.coin_notification)
            .setContentTitle("Minha Granna - ")
            .setAutoCancel(true)
            .setLights(Color.BLUE, 500, 500)
            .setVibrate(pattern)
            .setSound(alarmSound2)
            .setStyle(new NotificationCompat.InboxStyle())
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);

    if (NOTIFICATION_ID > 1073741824) {
        NOTIFICATION_ID = 0;
    }

    mNotificationManager.notify(NOTIFICATION_ID++, mBuilder.build());
    Log.d(TAG, "Notification sent successfully.");
}

}

公共类GcmBroadcastReceiver扩展WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(),
            GCMNotificationIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

<application
    android:name="lucasbergamo.minha_granna.Entidades.User"
    android:allowBackup="true"
    android:icon="@drawable/icones_indicador_financeiro"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name=".ac_Splash"
        android:label="@string/title_activity_splash" >
    </activity>
    <activity
        android:name=".ac_Login"
        android:label="@string/title_activity_splash" >

        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.intent.action.MAIN"/>
        </intent-filter>


    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />


    <receiver
        android:name=".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="lucasbergamo.minha_granna.Notification" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMNotificationIntentService" />


</application>

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />


<permission
    android:name="lucasbergamo.minha_granna.Notification.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />


<uses-permission android:name="lucasbergamo.minha_granna.Notification.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

只有连接USB电缆才会收到通知......如果没有,我会收到空​​白信息。

感谢!!!!

bergamo86@gmail.com skype lucazin

1 个答案:

答案 0 :(得分:0)

谢谢!包名不同!所以我只是设置当前包来解决这个问题。

全部谢谢!