简体   繁体   English

OneSignal:推送通知不起作用

[英]OneSignal: push notification doesn't work

I have this app and I want to send push notifications from my admin panel.我有这个应用程序,我想从我的管理面板发送推送通知。 I think I did everything OK, but OneSignal detects my app installed only when I install it in my emulator, but when I install the apk on my phone, it doesn't detect that.我想我做的一切都很好,但是 OneSignal 仅在我将其安装在模拟器中时检测到我的应用程序已安装,但是当我在手机上安装 apk 时,它没有检测到。 I have an android 10 phone.我有一部安卓 10 手机。 Here are my files:这是我的文件:

build.cradle (app): build.cradle(应用程序):

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/'}
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.8, 0.99.99]'
    }
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

repositories {
    maven { url 'https://maven.google.com' }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId 'ga.nabukoapps.materialwallpaper'

        manifestPlaceholders = [onesignal_app_id: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
                                // Project number pulled from dashboard, local value is ignored.
                                onesignal_google_project_number: "REMOTE"]

        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName '1.0'
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    useLibrary 'org.apache.http.legacy'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
    implementation 'com.google.android.ads.consent:consent-library:1.0.8'
    implementation 'com.balysv:material-ripple:1.0.2'
    //noinspection GradleDependency
    implementation 'com.onesignal:OneSignal:[3.15.0, 3.99.99]'
    //implementation 'com.onesignal:OneSignal:3.11.2'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
    implementation 'com.bogdwellers:pinchtozoom:0.1'
    implementation 'com.github.clans:fab:1.6.4'
    implementation 'com.karumi:dexter:4.2.0'
    implementation 'com.github.chrisbanes:PhotoView:2.1.4'
    implementation 'com.beloo.widget:ChipsLayoutManager:0.3.7@aar'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'commons-io:commons-io:2.4'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'

}

apply plugin: 'com.google.gms.google-services'

build.cradle (project): build.cradle(项目):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath 'com.google.gms:google-services:4.3.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

https://stackoverflow.com/a/39283454/8393749 https://stackoverflow.com/a/39283454/8393749

I've never worked with OneSignal, but one of the reasons this problem occurs is that the app package name the push is looking for is different.我从未使用过 OneSignal,但出现此问题的原因之一是推送要查找的应用程序包名称不同。 Maybe you can focus on that.也许你可以专注于此。

In the link above, there is a part of a build.gradle (app level) file you can take as a reference.在上面的链接中,您可以参考 build.gradle(应用程序级别)文件的一部分。 It mentions adding manifestApplicationId key and your app package name as a value in the manifestPlaceholders area like this:它提到将 manifestApplicationId 键和您的应用程序包名称添加为 manifestPlaceholders 区域中的值,如下所示:

release {
      defaultConfig {
         manifestPlaceholders = [manifestApplicationId          : "${applicationId}",
                                 onesignal_app_id               : "22222222-2222-2222-2222-222222222222",
                                 onesignal_google_project_number: "222222222"]
      }
   }

ps If you are not using any flavors, maybe you should remove this part from your code: ps 如果你没有使用任何口味,也许你应该从你的代码中删除这部分:

productFlavors {
}

I know it won't help you using OneSignal , but the best approach to work with notification in android is using Firebase Cloud Messaging , its well documented with a higher google-services compatibility since it belongs to Google .我知道它不会帮助您使用OneSignal ,但是在 android 中处理通知的最佳方法是使用Firebase Cloud Messaging ,由于它属于 Google ,因此它具有更高的谷歌服务兼容性。

Setting up Firebase Cloud Messaging will take you a couple of hours.设置 Firebase Cloud Messaging 需要几个小时。

To answer myself.来回答我自己。 Everything was done good on my side, but the problem is I guess in my phone.我这边一切都做得很好,但问题出在我的手机上。 I've read somewhere that it will not be a problem when I install the app from the Play Store.我在某处读到过,当我从 Play 商店安装应用程序时不会有问题。 Also, my emulator image was not good.另外,我的模拟器图像不好。 I've installed a new one, Pixel 3a I think, and it works fine in it.我已经安装了一个新的,我认为是 Pixel 3a,它运行良好。 Thanks everyone who tried to help.感谢所有试图提供帮助的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM