当从android中的设备清除内存时,正在进行的通知消失?

时间:2016-09-14 11:07:18

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

我的问题是每当我在我的Android设备的“任务管理器/ RAM”选项卡上清除内存时,我的通知就会消失。 即使我清除记忆,我还能做些什么来使我的持续通知保持不变?

这是我发送通知的类: -

public class GcmIntentService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    private final static String TAG = "GcmIntentService";
    private Context mContext;


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

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        Log.d(TAG, "Notification Data Json :" + extras.getString("message"));

        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)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.d(TAG, " Working... " + (i + 1) + "/5 @ "
                            + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();

                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                sendNotification(extras.getString("message"));
            }
        }        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }     // Put the message into a notification and post it.


    private void sendNotification(String msg) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);


        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("KEY","aman");


        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setContentTitle("Telepoh")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.setAutoCancel(true);


        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.gcm : R.drawable.push_icon;
    }
}

这是我的GcmBroadcastReceiver类: -

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

这是我的清单文件: -

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="package_name"
        android:versionCode="2"
        android:versionName="1.2">

        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.VIBRATE" />

        <permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.USE_CREDENTIALS" />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

        <permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <permission android:name="android.permission.GET_ACCOUNTS" />
        <permission android:name="android.permission.CAMERA" />

        <permission
            android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />

        <uses-permission android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" />

        <!-- This app has permission to register and receive data message. -->
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

        <application

            android:allowBackup="true"
            android:hardwareAccelerated="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:screenOrientation="portrait"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            tools:replace="android:icon">

            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="API_KEY" />

            <activity
                android:name=".WelcomeActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop"
                android:theme="@style/AppTheme.NoActionBar"></activity>
            <activity
                android:name=".LoginActivity"
                android:theme="@style/AppTheme.NoActionBar" />
            <activity
                android:name=".RegisterActivity"
                android:theme="@style/AppTheme.NoActionBar" />
            <activity android:name=".ForgetPasswordActivity" />


            <activity android:name="pacahe_name.controller.GalleryUtil" />

            <activity android:name="nl.changer.polypicker.ImagePickerActivity" />

            <receiver
                android:name="package_name.GCM.GcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>

                    <!-- Receives the actual messages. -->
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                    <category android:name=package_name" />
                </intent-filter>
            </receiver>


            <service android:name="package_name.GCM.GcmIntentService" />


            <activity
                android:name="com.facebook.FacebookActivity"
                android:screenOrientation="portrait" />

            <meta-data
                android:name="com.facebook.sdk.ApplicationId"
                android:value="@string/fb_id" />

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

1 个答案:

答案 0 :(得分:0)

尝试使用清单文件中的<action android:name="android.intent.action.PACKAGE_RESTARTED" />来关注此thread中的答案。

根据它,当您清除内存时调用PACKAGE_RESTARTED,并在清除内存后将其设置为重启接收器。

有关详细信息,请查看此documentation有关ACTION_PACKAGE_RESTARTED

的信息