Google Cloud Message - Notification USB Behavior

2019-09-03 16:24发布

问题:

Sorry, my english is not so good... anyway.

I have a android application that uses GCM to receive push notifications.

Steps: 1 - User click logon button 2 - Android app save the registration ID ( Working ) 3 - Send to WebService and put in database ( Working) 4 - Server Application (C#) send the message for selected registration id ( Working)

5 - Device receive the notification normally, BUT just if device is CONNECTED ON USB (* PROBLEM * )

if i dont connect the device on usb, i receive the notification but with a blank message.

i'm blocked for one week .. someone can help me ? Plz..

public class GCMNotificationIntentService extends 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.");
}

}

public class GcmBroadcastReceiver extends 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" />

I receive the notification only if the usb cable is attached...if not i will receive blank message.

Thanks!!!!

bergamo86@gmail.com skype lucazin

回答1:

Thanks! the package name are different ! so i fix this issue just setting the current package.

Thanks all!