I am with an ally in a flanking position around a foe. My ally is invisible and hasn't been detected by the foe.
Would I get the flanking bonus?
Would I get it even if I don't know about my ally being here?
Because even invisible you can threaten his square:
You threaten all squares into which you can make a melee attack, even when it is not your turn. Generally, that means everything in all squares adjacent to your space (including diagonally). An enemy that takes certain actions while in a threatened square provokes an attack of opportunity from you. If you're unarmed, you don't normally threaten any squares and thus can't make attacks of opportunity.
And that's all you need to gain the flanking bonus:
When making a melee attack, you get a +2 flanking bonus if your opponent is threatened by another enemy character or creature on its opposite border or opposite corner.
You will notice that the rules allow you to flank even mindless creatures, like oozes, animated objects, and plants. So flanking has little relation with whether or not the target feels threatened by his enemies.
However, these two rules were written back in 3.x and recieved no updates since. Yes, it makes little sense to feel distracted and threatened by something you don't even realize that is there. Or to be threatening a creature you don't wish to attack. Which is why it makes a lot more sense to consider being flanked as being distracted by multiple enemies during combat.
Personally, I only count as being threatened if the defender knows about the attackers, otherwise, the flanking bonus is replaced by the attacker invisible bonuses, but that's a house rule that only came up once since 3.x came out.
ShadowKras's answer nails the rules, but just in case there's lingering doubt, in a 2011 thread designer Stephen Radney-MacFarland had this exchange:
When GMing, I've always ruled that you don't provide flank[ing] if you're invisible or stealthed because the flanking rules are predicated on the target being "threatened." If I'm totally unaware of something's existence, it isn't going to be very threatening to me.
A creature threatens under certain circumstances (see page 180 of the Core Rulebook). It may not seem threatening to you, but that's not how threatened squares work in the rules. An invisible or hidden creature still threatens, because it could and might make and attack in a creature within those squares.
This same exchange is typically trotted out when the question arises on the Paizo messageboards, such as in 2013 and 2014.