I have a few questions about GCM (Google Cloud Messaging).
The first question is that: I am unable to start up the program. It seems to be always crashing:
The manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcmtutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<permission android:name="com.example.gcmtutorial.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcmtutorial.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.google.android.gcm.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="com.example.gcmtutorial" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The code is:
package com.example.gcmtutorial;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "714669202278");
} else {
Log.v("Registered", "Already registered");
}
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The error that I receive is that
07-27 18:11:23.504: E/AndroidRuntime(11390): FATAL EXCEPTION: main
07-27 18:11:23.504: E/AndroidRuntime(11390): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gcmtutorial/com.example.gcmtutorial.MainActivity}: java.lang.SecurityException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) } without permission com.google.android.c2dm.permission.RECEIVE
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.os.Looper.loop(Looper.java:156)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ActivityThread.main(ActivityThread.java:4987)
07-27 18:11:23.504: E/AndroidRuntime(11390): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 18:11:23.504: E/AndroidRuntime(11390): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 18:11:23.504: E/AndroidRuntime(11390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 18:11:23.504: E/AndroidRuntime(11390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 18:11:23.504: E/AndroidRuntime(11390): at dalvik.system.NativeStart.main(Native Method)
07-27 18:11:23.504: E/AndroidRuntime(11390): Caused by: java.lang.SecurityException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) } without permission com.google.android.c2dm.permission.RECEIVE
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ContextImpl.startService(ContextImpl.java:1356)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.content.ContextWrapper.startService(ContextWrapper.java:359)
07-27 18:11:23.504: E/AndroidRuntime(11390): at com.google.android.gcm.GCMRegistrar.internalRegister(GCMRegistrar.java:229)
07-27 18:11:23.504: E/AndroidRuntime(11390): at com.google.android.gcm.GCMRegistrar.register(GCMRegistrar.java:217)
07-27 18:11:23.504: E/AndroidRuntime(11390): at com.example.gcmtutorial.MainActivity.onCreate(MainActivity.java:24)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.Activity.performCreate(Activity.java:4538)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-27 18:11:23.504: E/AndroidRuntime(11390): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-27 18:11:23.504: E/AndroidRuntime(11390): ... 11 more
I have Google them up, but there seems to be no solution.
My second question is:
How can I write a php server using GCM
I have referred to:
GCM sending with curl (php)
GCM with PHP (Google Cloud Messaging)
The thing that I don't get is what does registration ID mean
try package.GCMIntentService in service in your manifest
Related
How to start a react-native app on device startup on Android. I have an Android app developed on react-native and if the app is running and user restarts his device, I wish to auto start my app.
Try using the react-native-autostart npm package.
I created a new .java file named BootReceiver.java:
package com.packageName;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
then I edited the AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.packageName.BootReceiver" android:enabled="true" android:exported="true">
<intent-filter >
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Im trying to use react-native-push-notifications. Its all working on iOS but on android I get undefined is not an object(evaluating RNPushnotification.getInitialNotifications).
Here's my build.gradle
dependencies {
compile project(':react-native-push-notification')
compile project(':react-native-vector-icons')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
compile project(':react-native-push-notification')
compile ('com.google.android.gms:play-services-gcm:8.1.0') {
force = true;
}
}
This is my manifestfile:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tzawajna"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.tzawajna.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.tzawajna" />
</intent-filter>
</receiver>
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
</manifest>
this is my MainApplication.java
package (My app);
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage()
);
}
};
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
I used react-native link and telling me that react-native-push-notifications has been linked. this is why i didnt added anything to the java file. I tried to add the asked code then the debuger tells me that theres an error with the javatojava
Please help.
I can see that the package is not linked to your java file.
add
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
at the first of your MainApplication.java file also add new ReactNativePushNotificationPackage() to getPackages method return value so it looks like this :
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePushNotificationPackage()
);
}
https://github.com/zo0r/react-native-push-notification/issues/728
worked for me.
compile project(':react-native-push-notification') was missing in build.gradle
I have a problem with the splashscreen on Android. Here is everything what I am doing:
Creating background_splash.xml file:
xml version="1.0" encoding="utf-8"?>
layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/darker_gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/splash"/>
</item>
I remove < in the first two lines because stackoverflow cannot display them. This file is located in drawable.
I added the following to styles.xml:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
and this is my manifest files:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>
And also the activity:
import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
public class SplashActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
This is the error I have:
Starting the app on 51d123cc (adb -s 51d123cc shell am start -n com.test/.MainActivity)...
Starting: Intent { cmp=com.test/.MainActivity }
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.test/.MainActivity } from null (pid=20601, uid=2000) not exported from uid 11152
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2589)
at com.android.commands.am.Am.runStart(Am.java:768)
at com.android.commands.am.Am.onRun(Am.java:307)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.am.Am.main(Am.java:102)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:255)
UPDATE:
To the answer of #Ankit Prajapati I will add also that I set
android:largeHeap="true"
which was an error from Android Studio (running from the terminal this error wasn't reported) and this happen after changing activity to SplashActivity.
Also it was necessary to change edit configurations and changing "Launch Activity" to "Specified Activity" and setting com.test.SplashActivity.
Second way of managing splash screen on Android and which I use right now is with this tutorial:
Splash Screen Android
Try using this in
android:exported="true" in the manifest file in the activity you are trying to start (Loading Activity)
Like this
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#style/SplashTheme"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Also Add
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
I am trying to get Branch.io to work on Android, but I am running into:
myapplication.MainActivity cannot be cast to android.app.Application
I then changed:
Branch.getAutoInstance(this);
To:
Branch.getInstance();
In onCreate of the Activity.
Then I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.branch.referral.Branch.initSession(io.branch.referral.Branch$BranchReferralInitListener, android.net.Uri, android.app.Activity)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
Can you help me get the basic up and running?
Following is my AndroidManifest.xml: (note: the branch_key is added in my app code)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.x.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxx" />
<activity android:name=".MainActivity">
<intent-filter>
<data android:scheme="yourapp" android:host="open" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="io.branch.referral.InstallListener" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
My main Activity:
package com.example.chg.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import org.json.JSONObject;
import io.branch.referral.Branch;
import io.branch.referral.BranchError;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Branch.getAutoInstance(this);
Branch.getInstance();
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
super.onStart();
Branch branch = Branch.getInstance();
branch.initSession(new Branch.BranchReferralInitListener(){
#Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
// params will be empty if no data found
// ... insert custom logic here ...
} else {
Log.i("MyApp", error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
#Override
public void onNewIntent(Intent intent) {
this.setIntent(intent);
}
}
Alex with Branch.io here:
We recently made some changes to our tutorial, and it looks like we missed a few things. I appreciate you posting about this — we'll be pushing an update later today for more clarity.
In this particular case, there are two issues:
A mixup between the Application onCreate() and Activity onCreate() methods, neither of which are actually needed for a basic implementation.
A missing Application class (we accidentally deleted this step from our tutorial completely — my apologies).
To get up and running, update your files as follows:
AndroidManifest.xml
You have three options here:
1. Use the Branch application class (easiest)
If you don't already have a custom application class, this is the simplest approach. Add android:name="io.branch.referral.BranchApp" to your Application class:
Edit: snippet UPDATED per comments below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chg.appbranch_01">
<meta-data android:name="io.branch.sdk.BranchKey" android:value="xxx" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="io.branch.referral.BranchApp">
<!--Enable test mode to see debugging data
(https://dev.branch.io/getting-started/integration-testing/guide/android/#use-debug-mode-to-simulate-fresh-installs)-->
<meta-data android:name="io.branch.sdk.TestMode" android:value="true" />
<meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="theapp" android:host="open" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<receiver android:name="io.branch.referral.InstallListener" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
2. Extend your Application class with the BranchApp class
If you already have a custom Application class, this is the simplest approach. Your AndroidManifext.xml file will look like this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="com.your.app.CustomApplicationClass" >
Your custom Application class (CustomApplicationClass in the example above) will look like this:
public final class CustomApplicationClass extends YourApp {
#Override
public void onCreate() {
super.onCreate();
}
}
3. Integrate directly into your custom application class
The most custom approach, for advanced implementations. You'd have your AndroidManifext.xml file set up the same as above:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="com.your.app.CustomApplicationClass" >
And then configure your Application class as follows:
public final class CustomApplicationClass {
#Override
public void onCreate() {
super.onCreate();
Branch.getAutoInstance(this);
}
}
Activity Definition
Remove the onCreate() calls. They aren't needed here and are actually the cause of your error message (Branch.getAutoInstance(this) was passing the Activity context as this, when the SDK was expecting the Application context from option 3 above).
import io.branch.referral.Branch;
import io.branch.referral.BranchError;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
super.onStart();
Branch branch = Branch.getInstance();
branch.initSession(new Branch.BranchReferralInitListener(){
#Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
// params will be empty if no data found
// ... insert custom logic here ...
} else {
Log.i("MyApp", error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
#Override
public void onNewIntent(Intent intent) {
this.setIntent(intent);
}
}
Sorry for the inconvenience!
So i have simple code, i just want to change layout when i screen orientation is changed.
package com.example.asdasd;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConfigurationChange(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.asdasd);
System.out.println("orientation---"+getResources().getConfiguration().orientation);
}
}
my manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asdasd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.asdasd.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So when i try this out on emulator and use ctrl+f12 or 9and 7 on num, screen flips but nothing happends and my layout stays the same, it wont work. Its like onConfigurationChange is never called and System.out.println is never reached.