仅在调试时才在设备上发布Android通知

时间:2014-04-21 19:54:46

标签: android notifications

我在Android设备上运行了一个奇怪的通知行为(运行v4.2.2)。当连接到调试器时,即使gcm.register(SENDER_ID)因SERVICE_NOT_AVAILABLE异常而失败,我也会收到通知。但是当设备从USB /调试器取消挂钩时,它不会收到任何通知。有任何想法吗?鉴于我在Android方面的经验有限,我对发生的事情感到非常困惑。此外,该设备正在运行Google Play Service v4.3.25。

这是我的清单 -

<?xml version="1.0" encoding="utf-8"?>

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

<uses-permission android:name="android.permission.INTERNET" />
<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" />

<permission
    android:name="com.example.cloudsample.app.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.cloudsample.app.permission.C2D_MESSAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher1"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
        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.example.cloudsample.app" />
        </intent-filter>
    </receiver>

    <activity
        android:name="com.example.cloudsample.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.cloudsample.app.MapsActivity"
        android:label="@string/title_activity_maps" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="MainActivity" />
    </activity>

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

</application>

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but are recommended.
-->
<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" />

使用NotificationCompat构建器来构建通知。

1 个答案:

答案 0 :(得分:0)

只是给出一些延迟来显示如下通知:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = null;
notificationIntent = new Intent(context, SplashScreen.class);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
RemoteViews notiview = new RemoteViews(getPackageName(), R.layout.notification_layout);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(icon)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setContent(notiview);
mBuilder.setContentIntent(intent);

new Timer().schedule(new TimerTask() {

    @Override
    public void run() {
         notificationManager.notify(0, mBuilder.build());

    }
}, 1000);