Android解析推送通知在某些设备上不起作用

Posted

技术标签:

【中文标题】Android解析推送通知在某些设备上不起作用【英文标题】:Android parse push notification not working in some devices 【发布时间】:2015-11-25 09:57:57 【问题描述】:

您好,我正在使用 android 的解析推送通知。我使用以下教程开发了应用程序。 http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/。我可以在一个棒棒糖设备上收到通知。我在 kitkat 和其他设备上没有收到通知。没找到问题。

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android_new_user.testnotification" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.example.android_new_user.testnotification.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.android_new_user.testnotification.permission.C2D_MESSAGE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android_new_user.testnotification.LoginActivity"
            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.android_new_user.testnotification.MainActivity"
            android:label="@string/app_name" />

        <service android:name="com.parse.PushService" />

        <receiver
            android:name=".CustomPushReciver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我的接收器文件看起来像

package com.example.android_new_user.testnotification;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.parse.ParsePushBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;
/**
 * Created by Android_new_user on 11/25/2015.
 */
public class CustomPushReciver extends ParsePushBroadcastReceiver 
    private final String TAG = CustomPushReciver.class.getSimpleName();

    private NotificationUtils notificationUtils;

    private Intent parseIntent;

    public CustomPushReciver() 
        super();
    

    @Override
    protected void onPushReceive(Context context, Intent intent) 
        super.onPushReceive(context, intent);

        if (intent == null)
            return;

        try 
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            Log.e(TAG, "Push received: " + json);

            parseIntent = intent;

            parsePushJson(context, json);

         catch (JSONException e) 
            Log.e(TAG, "Push message json exception: " + e.getMessage());
        
    

    @Override
    protected void onPushDismiss(Context context, Intent intent) 
        super.onPushDismiss(context, intent);
    

    @Override
    protected void onPushOpen(Context context, Intent intent) 
        super.onPushOpen(context, intent);
    

    /**
     * Parses the push notification json
     *
     * @param context
     * @param json
     */
    private void parsePushJson(Context context, JSONObject json) 
        try 
            boolean isBackground = json.getBoolean("is_background");
            JSONObject data = json.getJSONObject("data");
            String title = data.getString("title");
            String message = data.getString("message");

            if (!isBackground) 
                Intent resultIntent = new Intent(context, MainActivity.class);
                showNotificationMessage(context, title, message, resultIntent);
            

         catch (JSONException e) 
            Log.e(TAG, "Push message json exception: " + e.getMessage());
        
    



    private void showNotificationMessage(Context context, String title, String message, Intent intent) 

        notificationUtils = new NotificationUtils(context);

        intent.putExtras(parseIntent.getExtras());

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        notificationUtils.showNotificationMessage(title, message, intent);
    

和我的应用程序类

public class MyApplication extends Application 

    private static MyApplication mInstance;

    @Override
    public void onCreate() 
        super.onCreate();
        mInstance = this;

        // register with parse
        ParseUtils.registerParse(this);
    


    public static synchronized MyApplication getInstance() 
        return mInstance;
    

我该如何解决这个问题。请帮忙提前谢谢

【问题讨论】:

你能解决它吗? 【参考方案1】:

您是否已将应用程序名称添加到清单文件中?

还要检查清单中的以下代码

<permission
    android:name="yourpackagename.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="yourpackagename.permission.C2D_MESSAGE" />

    <receiver
        android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.parse.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="yourpackagename" />
        </intent-filter>
    </receiver>

【讨论】:

以上是关于Android解析推送通知在某些设备上不起作用的主要内容,如果未能解决你的问题,请参考以下文章

android firebase后台通知仅在vivo设备上不起作用

FCM 推送通知在 Android 4.4 上不起作用

推送通知声音在 iOS8 上不起作用

突然 fcm 通知在某些 iOS 设备上不起作用

Ionic 5 电容器:使用 ONESIGNAL 的推送通知在 iOS 上不起作用

推送通知在带有 release-apk 的 Android 7.0 (FCM) 上不起作用