仅在调试时设备上的Android通知

w9apscun  于 2023-04-18  发布在  Android
关注(0)|答案(2)|浏览(145)

我在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构建器构建通知。

5vf7fwbs

5vf7fwbs1#

只是给予一些延迟,以显示如下通知:

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);
q35jwt9p

q35jwt9p2#

如果您有一个service在此运行通知,您可以延迟startService:

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

    @Override
    public void run() {
         startService(intent);

    }
}, 1000);

相关问题