Android Notificatie

Status
Niet open voor verdere reacties.

murdoch201

Gebruiker
Lid geworden
31 mei 2008
Berichten
336
Hoi,

Ik heb wat problemen bij het maken van een notificatie voor een android app.
Ik wil dat hij de applicatie gewoon weer terug opent waar hij was gebleven wanneer de persoon op de notificatie klikt. Ik heb reeds tonnen aan forums afgelopen maar niks blijkt te werken.
Dit is wat ik nu heb:

Code:
long[] vpattern = {200,200,100,400}; 
    			        				NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
    			        				        .setSmallIcon(R.drawable.ic_launcher)
    			        				        .setContentTitle("Titeltje")
    			        				        .setNumber(++messageNum)
    			        				        .setOnlyAlertOnce(true)
    			        				        .setAutoCancel(true)
    			        				        .setDefaults (Notification.DEFAULT_SOUND)
    			        				        .setVibrate(vpattern)
    			        				        .setContentText("Berichtje");
    			        				// Creates an explicit intent for an Activity in your app
    			        				Context context = getApplicationContext();
    			        				Intent resultIntent = new Intent(context, MainActivity.class);
    			        				resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    			        				// The stack builder object will contain an artificial back stack for the
    			        				// started Activity.
    			        				// This ensures that navigating backward from the Activity leads out of
    			        				// your application to the Home screen.
    			        				TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
    			        				// Adds the back stack for the Intent (but not the Intent itself)
    			        				stackBuilder.addParentStack(MainActivity.class);
    			        				// Adds the Intent that starts the Activity to the top of the stack
    			        				stackBuilder.addNextIntent(resultIntent);


    			        				PendingIntent resultPendingIntent =
    			        				        stackBuilder.getPendingIntent(
    			        				            0,
    			        				            PendingIntent.FLAG_UPDATE_CURRENT
    			        				        );
    			        				mBuilder.setContentIntent(resultPendingIntent);
    			        				
    			        				
    			        				NotificationManager mNotificationManager =
    			        				    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    			        				// mId allows you to update the notification later on.
    			        				mNotificationManager.notify(2435, mBuilder.build());

Als ik hem nu draai werkt het ontvangen en optellen van notificaties perfect. Alleen als ik op de notificatie klik, crasht hij nu. Is afhankelijk van dit stukje:
Code:
Context context = getApplicationContext();
    			        				Intent resultIntent = new Intent(context, MainActivity.class);

Kan er iemand me helpen? :(

Groetjes,
murdoch201
 
Probeer jou code eens te vervangen door deze:

Code:
long[] vpattern = {200,200,100,400}; 
    			        				NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
    			        				        .setSmallIcon(R.drawable.ic_launcher)
    			        				        .setContentTitle("Titeltje")
    			        				        .setNumber(++messageNum)
    			        				        .setOnlyAlertOnce(true)
    			        				        .setAutoCancel(true)
    			        				        .setDefaults (Notification.DEFAULT_SOUND)
    			        				        .setVibrate(vpattern)
    			        				        .setContentText("Berichtje");
    			        				// Creates an explicit intent for an Activity in your app
    			        				Context context = getApplicationContext();
    			        				Intent resultIntent = new Intent(context, MainActivity.class);
    			        				resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    			        				// The stack builder object will contain an artificial back stack for the
    			        				// started Activity.
    			        				// This ensures that navigating backward from the Activity leads out of
    			        				// your application to the Home screen.
    			        				TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
    			        				// Adds the back stack for the Intent (but not the Intent itself)
    			        				stackBuilder.addParentStack(MainActivity.class);
    			        				// Adds the Intent that starts the Activity to the top of the stack
    			        				stackBuilder.addNextIntent(resultIntent);


    			        				PendingIntent resultPendingIntent =
*  			        				        stackBuilder.getActivity(this,
    			        				            0,
*													resultIntent,							
    			        				            PendingIntent.FLAG_UPDATE_CURRENT
    			        				        );
    			        				mBuilder.setContentIntent(resultPendingIntent);
    			        				
    			        				
    			        				NotificationManager mNotificationManager =
    			        				    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    			        				// mId allows you to update the notification later on.
    			        				mNotificationManager.notify(2435, mBuilder.build());
Bij het eerste sterretje heb ik getPendingIntent vervangen door getActivity (zie android documenation)
Bij het tweede sterretje heb ik resultIntent toegevoegd (dit moest omdat ik getActivity gebruik)

Zou je deze eens kunnen testen,
als hij werkt top!
Als hij niet werk gaan we er samen uitkomen ;-)
 
Hoi,

Hij werkt helaas niet. getActivity maakt helaas geen deel uit van de opties :(
 
Als je even de onderstaande code uitprobeert. Bij mij werkt hij in ieder geval!

Code:
public void notification(){
		int messageNum = 0;
		long[] vpattern = {200,200,100,400}; 
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
		        .setSmallIcon(R.drawable.ic_launcher)
		        .setContentTitle("Titeltje")
		        .setNumber(++messageNum)
		        .setOnlyAlertOnce(true)
		        .setAutoCancel(true)
		        .setDefaults (Notification.DEFAULT_SOUND)
		        .setVibrate(vpattern)
		        .setContentText("Berichtje");
	
		// Setting context
		Context context = MainActivity.this;
		
		// Setting the intent
		Intent resultIntent = new Intent(context, MainActivity.class);
		resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

		
		// The stack builder object will contain an artificial back stack for the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
		
		
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(MainActivity.class);
		
		
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);

		// 
		PendingIntent resultPendingIntent =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		mBuilder.setContentIntent(resultPendingIntent);
		
		
		NotificationManager mNotificationManager =
		    (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
		// mId allows you to update the notification later on.
		mNotificationManager.notify(2435, mBuilder.build());
	}
 
Helaas, hij crasht :(

Code:
07-13 22:40:06.518: E/AndroidRuntime(7816): FATAL EXCEPTION: main
07-13 22:40:06.518: E/AndroidRuntime(7816): Process: testapp.testje, PID: 7816
07-13 22:40:06.518: E/AndroidRuntime(7816): java.lang.RuntimeException: Unable to start activity ComponentInfo{testapp.testje/com.example.awmobile.MainActivity}: java.lang.NullPointerException
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.ActivityThread.access$800(ActivityThread.java:156)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.os.Handler.dispatchMessage(Handler.java:102)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.os.Looper.loop(Looper.java:157)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.ActivityThread.main(ActivityThread.java:5872)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at java.lang.reflect.Method.invoke(Method.java:515)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at dalvik.system.NativeStart.main(Native Method)
07-13 22:40:06.518: E/AndroidRuntime(7816): Caused by: java.lang.NullPointerException
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at com.example.awmobile.MainActivity.onCreate(MainActivity.java:98)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.Activity.performCreate(Activity.java:5312)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
07-13 22:40:06.518: E/AndroidRuntime(7816): 	... 11 more
 
Dat is jammer, maar we geven niet op!

Ik zie in jouw LogCat een NullPointerException staan, wat er op wijst dat er ergens een variabele (of method) geen waarde heeft. Dit gebeurt blijkbaar in je OnCreate method.
Code:
07-13 22:40:06.518: E/AndroidRuntime(7816): Caused by: java.lang.NullPointerException
07-13 22:40:06.518: E/AndroidRuntime(7816): 	at com.example.awmobile.MainActivity.onCreate(MainActivity.java:98)

Zou je je gehele OnCreate method willen posten? Dan is het wat makkelijker te zien waar de waarde plots null zou zijn.
 
Ik heb dat deel opgelost met try & catch. Maar nu zit ik het met volgende: hij doet MainActivity helemaal over. Hij toont dus een heel ander menu dan dat ik zou moeten krijgen.

Is het mogelijk zodat Mainactivty niet nog eens volledig wordt uitgevoerd? (ook een boolean met IfApplicationWasAlreadyRunning en if's mag)
 
Sorry voor het late berichtje..
Ik snap niet helemaal wat je bedoelt met het "MainActivity over doen". Zou je een screenshot willen laten zien met wat je precies bedoelt?

Alvast bedankt!
 
Nou dit staat bvb vermeld in MainActivity:

editText1.setVisibility(View.INVISIBLE);
editText2.setVisibility(View.INVISIBLE);
textView2.setVisibility(View.INVISIBLE);
connectButton.setVisibility(View.INVISIBLE);
spinner1.setVisibility(View.INVISIBLE);
progressBar1.setVisibility(View.VISIBLE);

en in activity_main.xml:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="Login" />

Hij blijkt dit overnieuw te doen. Normaal gezien is het een chat applicatie. Van zodra de gebruiker inlogt, verdwijnen de controls voor in te loggen, en verschijnen de chat-controls. Maar als je nu dus op die chat melding klikt, zijn ineens de chat-controls weer verdwenen en de login-controls terug zichtbaar.
 
Oke, nu snap ik hem!

Dit komt door de resultIntent in de code:

Code:
// Setting the intent
		Intent resultIntent = new Intent(context, MainActivity.class);
		resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);


Op het moment dat je dus op de notificatie klikt, dan zal er een Intent gestart worden die je bij de resultIntent aangeeft. In dit geval is dit dus de MainActivity.class.
Als je dit niet wilt, kan je de volgende code weglaten, of je kan de resultIntent naar de gewenste Intent klasse zetten:

Code:
	// Creates an explicit intent for an Activity in your app
    			        				Context context = getApplicationContext();
    			        				Intent resultIntent = new Intent(context, MainActivity.class);
    			        				resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    			        				// The stack builder object will contain an artificial back stack for the
    			        				// started Activity.
    			        				// This ensures that navigating backward from the Activity leads out of
    			        				// your application to the Home screen.
    			        				TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
    			        				// Adds the back stack for the Intent (but not the Intent itself)
    			        				stackBuilder.addParentStack(MainActivity.class);
    			        				// Adds the Intent that starts the Activity to the top of the stack
    			        				stackBuilder.addNextIntent(resultIntent);


    			        				PendingIntent resultPendingIntent =
*  			        				        stackBuilder.getActivity(this,
    			        				            0,
*													resultIntent,							
    			        				            PendingIntent.FLAG_UPDATE_CURRENT
    			        				        );
    			        				mBuilder.setContentIntent(resultPendingIntent);
 
Oke, en is het in je applicatie mogelijk om een nieuwe activity te maken, zodat daarop kunt komen als je ingelogd bent?
Als je dan een andere View hebt in de nieuwe Activity, en je zet de Intent op die nieuwe activity, dan heb je het probleem niet meet dat alles weer opnieuw begint (mainactivity wordt dan overgeslagen).

Het bovenstaande lijkt mij het handigst om te doen. Ik zou dan alle taken die er nodig zijn voor het chatten, overdragen naar de nieuwe activity.

Laat me even weten of dit een optie voor je is!
 
Het overplaatsen is praktisch onmogelijk. Dit wil zeggen dat ik de hele code opnieuw moet schrijven (en het is over de 1000 regels). Ik heb reeds geprobeerd om vanuit een andere class de layout van MainActivity te laden, en zo de controls weer zichtbaar maken. Ook zonder succes.

Code:
package com.example.XXXXXXXX;

import murdoch3d.XXXXXXX.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

public class NotifClick extends Activity {
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	
	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    
    final EditText editText1 = (EditText) NotifClick.this.findViewById(R.id.editText1);
    final EditText editText2 = (EditText) NotifClick.this.findViewById(R.id.editText2);
    final TextView textView3 = (TextView) NotifClick.this.findViewById(R.id.textView3);
    final EditText editText4 = (EditText) NotifClick.this.findViewById(R.id.editText4);
    final TextView textView1 = (TextView) NotifClick.this.findViewById(R.id.textView1);
    final TextView textView2 = (TextView) NotifClick.this.findViewById(R.id.textView2);
    final ProgressBar progressBar1 = (ProgressBar) NotifClick.this.findViewById(R.id.progressBar1);
    final Button button1 = (Button) NotifClick.this.findViewById(R.id.button1);
    final Button button2 = (Button) NotifClick.this.findViewById(R.id.button2);
final Button connectButton = (Button) NotifClick.this.findViewById(R.id.button1);
final ListView listView1 = (ListView) NotifClick.this.findViewById(R.id.listviewb);
final WebView webView1 = (WebView) NotifClick.this.findViewById(R.id.webView1);
    final Spinner spinner1 = (Spinner) NotifClick.this.findViewById(R.id.spinner1);
    
    
    textView3.setVisibility(View.VISIBLE);
	editText4.setVisibility(View.VISIBLE);
	button2.setVisibility(View.VISIBLE);
	progressBar1.setVisibility(View.INVISIBLE);
	textView1.setVisibility(View.INVISIBLE);
	textView2.setVisibility(View.INVISIBLE);
	editText4.requestFocus();
	}
	
}
 
Ik heb even rondgezocht naar een oplossing voor jouw probleem. Uiteindelijk kwam ik uit bij een antwoord van StackOverflow om bij de Intent een putExtra te zetten, met daarin een boolean.
In je onCreate maak je dan een IF statement met daarin een check voor de boolean. Indien deze voorkomt, dan wordt de visibility uitgevoerd.
Kijk maar even of het iets voor je is, anders hoor ik het weer graag!

http://stackoverflow.com/a/21886366/3603639
 
Helaas, hij reageert nu wel als ik op de notificatie klik, maar hij doet nu ook de app open van zodra hij dus een bericht ontvangt uit zichzelf.


Oncreate:
Code:
boolean notif = getIntent().getBooleanExtra("notif", false);
		if(notif)
		{
			textView3.setVisibility(View.VISIBLE);
			editText4.setVisibility(View.VISIBLE);
			button2.setVisibility(View.VISIBLE);
			progressBar1.setVisibility(View.INVISIBLE);
			textView1.setVisibility(View.INVISIBLE);
			textView2.setVisibility(View.INVISIBLE);
			editText4.requestFocus();
			return;
			
		}

Bericht wordt ontvangen:
Code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
    			        				        .setSmallIcon(R.drawable.ic_launcher)
    			        				        .setContentTitle("Titeltje")
    			        				        .setNumber(++messageNum)
    			        				        .setOnlyAlertOnce(true)
    			        				        .setAutoCancel(true)
    			        				        .setDefaults (Notification.DEFAULT_SOUND)
    			        				        .setVibrate(vpattern)
    			        				        .setContentText(ontvangenBericht);
    			        			
    			        				// Setting context
    			        				Context context = MainActivity.this;
    			        				
    			        				// Setting the intent
    			        				Intent resultIntent = new Intent(context, MainActivity.class);
    			        				resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    			        				resultIntent.putExtra("notif", true);
    			        				startActivity(resultIntent);  
    			        				
    			        				// The stack builder object will contain an artificial back stack for the
    			        				// started Activity.
    			        				// This ensures that navigating backward from the Activity leads out of
    			        				// your application to the Home screen.
    			        				TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    			        				
    			        				
    			        				// Adds the back stack for the Intent (but not the Intent itself)
    			        				stackBuilder.addParentStack(MainActivity.class);
    			        				
    			        				
    			        				// Adds the Intent that starts the Activity to the top of the stack
    			        				stackBuilder.addNextIntent(resultIntent);

    			        				// 
    			        				PendingIntent resultPendingIntent =
    			        						stackBuilder.getPendingIntent(
        			        				            0,
        			        				            PendingIntent.FLAG_UPDATE_CURRENT
        			        				        );

    			        				mBuilder.setContentIntent(resultPendingIntent);
    			        				
    			        				
    			        				NotificationManager mNotificationManager =
    			        				    (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
    			        				// mId allows you to update the notification later on.
    			        				mNotificationManager.notify(2435, mBuilder.build());
 
Excuses voor het late berichtje,
Ben net terug van vakantie en zal rond de namiddag weer even kijken!

Groet,
Kjell
 
Ik heb een beetje rondgezocht op het internet voor dit probleem, maar ben helaas niet wijzer van geworden haha.

Als ik nu naar de code kijk, lijkt het mij een probleem met de boolean "notif".
Volgens mij kan de visibilitynu ook nooit meer "false" worden, aangezien hij altijd dezelfde waarde zal ontvangen van de intent.
Er moet dus ook een else zijn voor de code.

Als je de boolean weghaalt, opent de app zichzelf na de notificatie dan niet meer? Op deze manier kunnen we het probleem namelijk isoleren.
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan